简体   繁体   English

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

[英]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.

相关问题 如何使用C#语言显示5位(十六进制)字符Unicode - How to present a character Unicode with 5 digit (Hex) with c# language 如何在 C# 中获取 unicode 字符的十进制值? - How do i get the decimal value of a unicode character in C#? 如何在 C# 控制台中显示扩展的 Unicode 字符? - How can I display extended Unicode character in a C# console? 如何获取C#中的十六进制值? - How can I get the hex value in C#? C#Unicode空表示代码字符数组 - c# Unicode Null Representation code character array 字体是对的。为什么我不能在这个C#控制台应用程序中显示这个unicode字符? - Font is right. Why can't I get this unicode character to display in this C# console app? 如何替换仅具有unicode表示形式的字符? - How can I replace a character from which I only have the unicode representation? 有没有办法将 Unicode 十六进制字符代码 a 转换为简单的“a”,以便我可以得到一个字母字符串? - Is there a way to covert Unicode Hex Character Code a to a simple "a" so that I can get an alphabetic string? 如何在 C# 控制台应用程序中获取光标处的字符? - How can I get the character at the cursor in a C# console application? 如何摆脱 C# 中的字符重复延迟? - How can I get rid of character repeat delay in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM