makerofthings7
makerofthings7

Reputation: 61463

How do I encode a Binary blob as Unicode blob?

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.

Upvotes: 3

Views: 3498

Answers (2)

Venemo
Venemo

Reputation: 19067

Normally, this would be the way to convert between bytes and Unicode text:

// string from bytes
System.Text.Encoding.Unicode.GetString(bytes);

// bytes from string
System.Text.Encoding.Unicode.GetBytes(bytes);

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:

// string from bytes
Convert.ToBase64String(byteArray);

// bytes from string
Convert.FromBase64String(base64Encoded);

(Thanks to @Timwi who pointed this out!)

Upvotes: 1

Timwi
Timwi

Reputation: 66573

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:

var base64Encoded = Convert.ToBase64String(byteArray);

var original = Convert.FromBase64String(base64Encoded);

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.

Upvotes: 4

Related Questions