首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#将十六进制值转换为UTF8和ASCII码

C#将十六进制值转换为UTF8和ASCII码
EN

Stack Overflow用户
提问于 2013-09-26 17:49:53
回答 3查看 10.5K关注 0票数 4

我正在尝试将字符串中的十六进制值转换为ASCII值和UTF8值。但是,当我执行以下代码时,它会打印出与输入相同的十六进制值

代码语言:javascript
复制
string hexString = "68656c6c6f2c206d79206e616d6520697320796f752e";
System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
byte[] dBytes = encoding.GetBytes(hexString);

//To get ASCII value of the hex string.
string ASCIIresult = System.Text.Encoding.ASCII.GetString(dBytes);
MessageBox.Show(ASCIIresult, "Showing value in ASCII");

//To get the UTF8 value of the hex string
string utf8result = System.Text.Encoding.UTF8.GetString(dBytes);
MessageBox.Show(utf8result, "Showing value in UTF8");
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-26 18:09:19

由于您将变量命名为hexString,因此我假设该值已经编码为十六进制格式。

这意味着以下内容将不起作用:

代码语言:javascript
复制
string hexString = "68656c6c6f2c206d79206e616d6520697320796f752e";
System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
byte[] dBytes = encoding.GetBytes(hexString);

这是因为您将已经编码的字符串视为纯UTF8文本。

您可能缺少将十六进制编码的字符串转换为字节数组的步骤。

您可以使用this SO post来完成此操作,其中显示了此函数:

代码语言:javascript
复制
public static byte[] StringToByteArray(String hex)
{
  int NumberChars = hex.Length/2;
  byte[] bytes = new byte[NumberChars];
  using (var sr = new StringReader(hex))
  {
    for (int i = 0; i < NumberChars; i++)
      bytes[i] = 
        Convert.ToByte(new string(new char[2]{(char)sr.Read(), (char)sr.Read()}), 16);
  }
  return bytes;
}

因此,最终结果将是:

代码语言:javascript
复制
byte[] dBytes = StringToByteArray(hexString);

//To get ASCII value of the hex string.
string ASCIIresult = System.Text.Encoding.ASCII.GetString(dBytes);
MessageBox.Show(ASCIIresult, "Showing value in ASCII");

//To get the UTF8 value of the hex string
string utf8result = System.Text.Encoding.UTF8.GetString(dBytes);
MessageBox.Show(utf8result, "Showing value in UTF8");
票数 5
EN

Stack Overflow用户

发布于 2013-09-26 18:02:03

您应该首先将十六进制字符串转换为字节数组:

代码语言:javascript
复制
byte[] dBytes = Enumerable.Range(0, hexString.Length)
                 .Where(x => x % 2 == 0)
                 .Select(x => Convert.ToByte(hexString.Substring(x, 2), 16))
                 .ToArray();
票数 3
EN

Stack Overflow用户

发布于 2021-06-04 12:27:10

我已经使用此方法将任何

代码语言:javascript
复制
public static string FromHex (string h) //Put your sequence of hex to convert to string.
{
    if (h.Length % 2 != 0) 
        throw new ArgumentException("The string " + nameof(h) + " is not a valid Hex.", nameof(h));
    char[] CharFromHex = new char[h.Length / 2];
    int j = 0;
    for (int i = 0; i < h.Length; i += 2)
    {
        string hexSubStr = h.Substring(i, 2);
        CharFromHex[j] = (char)Convert.ToInt32(hexSubStr, 16);
        j += 1;
    }
    StringBuilder str = new StringBuilder();
    str.Append(CharFromHex);
    return str.ToString();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19024925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档