如何将字符串转换为 OctetString (C#)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/887734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 08:30:12  来源:igfitidea点击:

How do I convert a string into an OctetString (C#)?

c#ldapattributes

提问by Adrian

I'm importing data into an LDAP store. The attribute in question is of type OctetString. I have a normal string that I need to get into that attribute.

我正在将数据导入 LDAP 存储。有问题的属性是 OctetString 类型。我有一个需要进入该属性的普通字符串。

I'm using C# (.net 3.5)

我正在使用 C# (.net 3.5)

How do I do it?

我该怎么做?

采纳答案by Eoin Campbell

As far as I know, an OctetString is just a byte array. Not to be confused with Octal (base 8)

据我所知, OctetString 只是一个字节数组。不要与八进制混淆(基数为 8)

Completely open to correction but some random googling would seem to agree... This will convert your string to a byte array

完全可以更正,但一些随机的谷歌搜索似乎同意......这会将您的字符串转换为字节数组

byte[] octets = System.Text.Encoding.ASCII.GetBytes("abcd"); 

回答by Noldorin

An OctetStringappears to simply be a string represented as an array of bytes (hence octets).

AnOctetString似乎只是一个表示为字节数组的字符串(因此octets)。

The MSDN page on Octet String (SID) Property Typeprovides the following example for writing an Octet String:

八位字节字符串 (SID) 属性类型的 MSDN 页面提供了以下用于编写八位字节字符串的示例:

byte[] usrSID = (byte[])usr.Properties["objectSid"].Value;
usr.Properties["objectSid "].Clear();
usr.Properties["objectSid "].Value = usrSID;
usr.CommitChanges();

回答by Demi

System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
byte[] octets = utf8.GetBytes("Réne");