如何将二进制 blob 编码为 Unicode blob?
我正在尝试将 Gzip 序列化对象存储到 Active Directory 的“扩展属性”中,更多信息 此处。该字段是一个 Unicode 字符串,其 oM 语法为 64。
将二进制 blob 存储为 Unicode 的最有效方法是什么?一旦我把这个记下来,剩下的就是小菜一碟了。
I'm trying to store a Gzip serialized object into Active Directory's "Extension Attribute", more info here. This field is a Unicode string according to it's oM syntax of 64.
What is the most efficient way to store a binary blob as Unicode? Once I get this down, the rest is a piece of cake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,有很多方法可以可靠地将任意字节数组打包为 Unicode 字符,但没有一种方法非常有效。 非常不幸,ActiveDirectory 选择对非文本数据使用 Unicode。这就像用一个字符串来表示一个 32 位整数,或者像用 Nutella 来写一封情书。
我的建议是“谨慎行事”并使用基于 ASCII 的编码,例如
base64
。我建议这样做的原因是因为已经有一个内置的 .NET 实现可以实现这一点:理论上,您可以通过更多地使用 Unicode 字符集来想出比这更有效的编码。然而,为了可靠地做到这一点,您需要对 Unicode 有相当多的了解。
There are, of course, many ways of reliably packing an arbitrary byte array into Unicode characters, but none of them are very efficient. It is very unfortunate that ActiveDirectory would choose to use Unicode for data that is not textual in nature. It’s like using a string to represent a 32-bit integer, or like using Nutella to write a love letter.
My recommendation would be to “play it safe” and use an ASCII-based encoding such as
base64
. The reason I recommend this is because there is already a built-in .NET implementation for this:In theory you could come up with an encoding that is more efficient than this by making use of more of the Unicode character set. However, in order to do so reliably, you would need to know quite a bit about Unicode.
通常,这将是在字节和 Unicode 文本之间转换的方式:
编辑:
但由于并非每个可能的字节序列都是有效的 Unicode 字符串,因此您应该使用一种可以从任意字节序列创建字符串的方法:(
感谢 @Timwi 指出了这一点!)
Normally, this would be the way to convert between bytes and Unicode text:
EDIT:
But since not every possible byte sequence is a valid Unicode string, you should use a method that can create a string from an arbitrary byte sequence:
(Thanks to @Timwi who pointed this out!)