What's the best sequence to compress binary data, encode it, and convert to string?
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 th开发者_开发问答e 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?
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:
byte[] bytes = ... // This is my blob
System.Convert.ToBase64String(bytes);
You then use:
System.Convert.ToBase64String(string);
To get your data back as bytes.
This is definitely a safer approach than what you are doing.
精彩评论