我正在尝试将 gzip 压缩的二进制序列化对象存储到 Active Directory 的“扩展属性”中,更多信息 此处。该字段是一个 Unicode 字符串,其 oM 语法为 64。
我将二进制对象保存为 AD 的 Unicode 格式,如下所示:
byte[] bytes = ... // This is my blob
System.Text.Encoding.Unicode.GetString(bytes);
然后将其保存到扩展属性 #14。问题是,当我读取该值时,我没有取回整个字符串。
以下是实际保存到服务器的内容的屏幕截图:
这是返回的屏幕截图:
我猜测 \0 导致了问题,这可能意味着 null。我应该如何处理这个问题?除了 null 之外,我还应该转义其他字符吗?
I'm trying to store a gzipped binary 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.
I'm saving the binary object into AD's Unicode format like this:
byte[] bytes = ... // This is my blob
System.Text.Encoding.Unicode.GetString(bytes);
I then save it to extension attribute #14. The issue is that when I read the value I don't get my entire string back.
Here is a screenshot of what is actually saved to the server:
Here is a screenshot of what comes back:
I am guessing that the \0 is causing the problem, and that probably means null. How should I handle this? Are there other chars I should also be escaping besides null?
发布评论
评论(1)
我假设您正在尝试将二进制数据放入字符串字段中。
简单地将数据从二进制转换为 Unicode 在某种程度上是一个坏主意(这就是您遇到的原因之一,但 Null (0) 并不是 Unicode 字符串编码中唯一可能给您带来问题的点。有其他控制字符,您可能有指向 Unicode 中保留的字符的字节对,等等)
我建议考虑使用 Base64。它就是为了这个目的而设计的。虽然这可能会妨碍您使用 gzip 进行压缩,但它应该可以解决您的问题。
您的代码将类似于:
然后您使用:
将数据以字节形式返回。
这绝对是比您正在做的更安全的方法。
I assume you're trying to put binary data into a string field.
Simply converting the data from binary to Unicode is somewhat of a bad idea (one of which is the reason you've encountered, but Null (0) isn't the only point in Unicode string encoding which may cause issues for you. There are other control characters, you might have byte pairs that point to characters that are reserved in Unicode, etc.)
I would recommend considering Base64 instead. It was designed for this exact purpose. While this probably hinders your compression efforts using gzip, it should solve your problem.
Your code will instead be something like:
You then use:
To get your data back as bytes.
This is definitely a safer approach than what you are doing.