繁体   English   中英

如何在C#中获取Unicode字符的十六进制表示形式?

[英]How can I get the Hex representation of a Unicode character in C#?

这是我的代码:

var hexString = BitConverter.ToString(Encoding.Unicode.GetBytes("ABCD"));

hexString = Regex.Replace(hexString, "[-]", string.Empty);

我得到结果:

4100420043004400

我应该得到:

0041004200430044

看起来像是尾随的,以“ 00”结尾的问题,但是在尝试使用特殊字符时实际上却遇到了其他错误。

例如希腊字母Ο

应该是039f但是我的代码给了我9F03

我什至尝试了以下在另一个问题中发现的代码:

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();
}

结果是一样的。

您将在Little Endian中获得Unicode编码。

尝试使用Encoding.BigEndianUnicode-它会为您提供所需的内容( 0041004200430044对于“ ABCD”为0041004200430044 )-只需将Unicode更改为BigEndianUnicode。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM