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.
You get Unicode encoded in Little Endian .
Try Encoding.BigEndianUnicode - it will give you what you want (ie 0041004200430044
for 'ABCD') - just change Unicode to BigEndianUnicode.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.