简体   繁体   English

C#十六进制转ascii

[英]C# hex to ascii

I'm trying to convert a String of hex to ASCII, using this:我正在尝试使用以下方法将十六进制字符串转换为 ASCII:

public void ConvertHex(String hexString)
{
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < hexString.Length; i += 2)
    {
        String hs = hexString.Substring(i, i + 2);
        System.Convert.ToChar(System.Convert.ToUInt32(hexString.Substring(0, 2), 16)).ToString();
    }
    String ascii = sb.ToString();
    MessageBox.Show(ascii);
}

but I get an out or bounds exception, I'm sure its a glaring error but other code I have tried does not work either.但我得到一个 out 或 bounds 异常,我确定这是一个明显的错误,但我尝试过的其他代码也不起作用。 What am I doing wrong?我究竟做错了什么?

This code will convert the hex string into ASCII, you can copy paste this into a class and use it without instancing此代码会将十六进制字符串转换为 ASCII,您可以将其复制粘贴到一个类中并使用它而无需实例化

public static string ConvertHex(String hexString)
{
    try
    {
        string ascii = string.Empty;

        for (int i = 0; i < hexString.Length; i += 2)
        {
            String hs = string.Empty;

            hs   = hexString.Substring(i,2);
            uint decval =   System.Convert.ToUInt32(hs, 16);
            char character = System.Convert.ToChar(decval);
            ascii += character;

        }

        return ascii;
    }
    catch (Exception ex) { Console.WriteLine(ex.Message); }

    return string.Empty;
}

Notes笔记

2 = the no. 2 = 没有。 of hexString chars used to represent an ASCII character.用于表示 ASCII 字符的 hexString 字符。

System.Convert.ToUInt32(hs, 16) = "convert the base 16 hex substrings to an unsigned 32 bit int" System.Convert.ToUInt32(hs, 16) = "将基本的 16 个十六进制子字符串转换为无符号的 32 位整数"

There arefourthree problems here:这里的三个问题:

  1. Since you're incrementing i by 2 on each iteration, you need to terminate at hexString.Length - 1 .由于您在每次迭代时将i增加 2,因此您需要在hexString.Length - 1处终止。 This doesn't actually matter;这实际上并不重要; incrementing by two after the final iteration will bring the counter above the checked length regardless.无论如何,在最后一次迭代后增加 2 将使计数器高于检查长度。
  2. You're taking the wrong number of characters from hexString .您从hexString获取了错误的字符hexString
  3. hs is never used. hs从未使用过。
  4. You're not appending anything to sb .您没有向sb附加任何内容。

Try this:尝试这个:

for (int i = 0; i < hexString.Length; i += 2)
{
    string hs = hexString.Substring(i, 2);
    sb.Append(Convert.ToChar(Convert.ToUInt32(hs, 16)));
}

Note that there's no need to qualify the types with their namespace, System (assuming you've referenced it at the top of the file with a using statement).请注意,不需要使用命名空间System来限定类型(假设您已经using语句在文件顶部引用了它)。

String hs = hexString.Substring(i, i + 2);
System.Convert.ToChar(System.Convert.ToUInt32(hexString.Substring(0, 2), 16)).ToString();

Do you notice you're never using hs ??你有没有注意到你从来没有使用过hs

And that you're converting the first 2 chars over and over?并且您一遍又一遍地转换前两个字符?

Since you are incrementing your index by 2, you need to stop your loop one-before-the-end of the length of the string.由于您将索引增加 2,因此您需要在字符串长度的末尾处停止循环。 Otherwise your last iteration of the loop will try to read characters past the end of the string.否则,循环的最后一次迭代将尝试读取超过字符串末尾的字符。

for (int i = 0; i < hexString.Length - 1, i += 2)

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

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