简体   繁体   English

十六进制字符串的字节转换在C#中始终无法使用字母字符“ G”及更高版本

[英]Byte Conversion From Hex String Keeps Failing With Alphabet characters “G” and upwards in C#

I'm trying to build a simple XOR function that decrypts a hex key from a single character. 我正在尝试构建一个简单的XOR函数,该函数从单个字符中解密一个十六进制密钥。 It works with multiple characters and individual characters... but only from "A" through to "F". 它适用于多个字符和单个字符...但是只能从“ A”到“ F”。 From "G" onwards, it keeps throwing a "FormatException". 从“ G”开始,它一直抛出“ FormatException”。

Here's the problem function, passed with two strings: 这是有两个字符串传递的问题函数:

    // For Reference:
    // strHexKey = "G";
    // strHexInput = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"

    private string SCXOR_Update(string strHexKey, string strHexInput)
    {
        byte[] buffer1 = hexbuffer(strHexInput);
        byte buffer2 = Convert.ToByte(Convert.ToInt32(strHexKey, 16));
        byte[] result = buffer1;

        for (int i = 0; i < (strHexInput.Length / 2); i++)
        {
            result[i] = (byte)(buffer1[i] ^ buffer2);
        }

        return BitConverter.ToString(result).Replace("-", "");
    }

The exception is thrown at the line "byte buffer2" and only happens when advancing from the letter "G" upwards. 该异常抛出在“字节缓冲区2”行,仅在从字母“ G”向上前进时发生。

Any idea what it is that I'm missing? 知道我想念的是什么吗? It's got to be glaring me in the face?! 一定要瞪着我吗?!

Thanks in advance! 提前致谢!

Hex is a base 16 counting system . 十六进制是基数为16的计数系统 It only consists of the following digits - although lowercase characters are often used as well: 它仅由以下数字组成-尽管也经常使用小写字符:

0123456789ABCDEF

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

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