[英]How can I get the Hex representation of a Unicode character in C#?
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. 看起来像是尾随的,以“ 00”结尾的问题,但是在尝试使用特殊字符时实际上却遇到了其他错误。
For instance the greek letter Ο 例如希腊字母Ο
Should be 039f
but my code gives me 9F03
应该是039f
但是我的代码给了我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 . 您将在Little Endian中获得Unicode编码。
Try Encoding.BigEndianUnicode - it will give you what you want (ie 0041004200430044
for 'ABCD') - just change Unicode to BigEndianUnicode. 尝试使用Encoding.BigEndianUnicode-它会为您提供所需的内容( 0041004200430044
对于“ ABCD”为0041004200430044
)-只需将Unicode更改为BigEndianUnicode。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.