簡體   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