Reputation: 2815
Here is my code:
var hexString = BitConverter.ToString(Encoding.Unicode.GetBytes("ABCD"));
hexString = Regex.Replace(hexString, "[-]", string.Empty);
I get the result:
4100420043004400
I should get:
0041004200430044
That looks like a trailing, ending with "00" problem but I actually get other errors when trying it with special characters.
For instance the greek letter Ο
Should be 039f
but my code gives me 9F03
I even tried the below code that I found in another question:
public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
The result is the same.
Upvotes: 0
Views: 190
Reputation: 517
You get Unicode encoded in Little Endian.
Try Encoding.BigEndianUnicode - it will give you what you want (i.e. 0041004200430044
for 'ABCD') - just change Unicode to BigEndianUnicode.
Upvotes: 4