Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#拆分六角字符串_C#_Arrays_Split_Hex_Byte - Fatal编程技术网

C#拆分六角字符串

C#拆分六角字符串,c#,arrays,split,hex,byte,C#,Arrays,Split,Hex,Byte,嗯,我得到了我的十六进制字符串(数据包),例如“70340A01000000000000”,我每次都想在2个字符后进行拆分,并将其放入字节数组(流) 表示{70,34,0A,01,00,00,00,00,00,00,00}最短路径(.NET 4+)是(取决于长度或原点): 对于以前的版本,string.length/2还定义了字节数组的长度,而不是每个解析对可以填充的长度。这个字节数组可以如上所述写入流 对于这两种情况,如果您的源字符串太长,并且您希望避免一个巨大的字节数组,请继续执行步骤,从源

嗯,我得到了我的十六进制字符串(数据包),例如“70340A01000000000000”,我每次都想在2个字符后进行拆分,并将其放入字节数组(流)

表示{70,34,0A,01,00,00,00,00,00,00,00}

最短路径(.NET 4+)是(取决于长度或原点):

对于以前的版本,string.length/2还定义了字节数组的长度,而不是每个解析对可以填充的长度。这个字节数组可以如上所述写入流


对于这两种情况,如果您的源字符串太长,并且您希望避免一个巨大的字节数组,请继续执行步骤,从源字符串中获取N个字符组。

这实际上非常有效!我很抱歉,如果你的代码做相同的,但我只是不明白

public static byte[] ConvertHexStringToByteArray(string hexString)
        {
            if (hexString.Length % 2 != 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
            }

            byte[] HexAsBytes = new byte[hexString.Length / 2];
            for (int index = 0; index < HexAsBytes.Length; index++)
            {
                string byteValue = hexString.Substring(index * 2, 2);
                HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
            }

            return HexAsBytes;
public static byte[]ConvertHexStringToByteArray(字符串hexString)
{
如果(hexString.Length%2!=0)
{
抛出新的ArgumentException(String.Format(CultureInfo.InvariantCulture,“二进制键不能有奇数位数:{0}”,hexString));
}
byte[]HexAsBytes=新字节[hexString.Length/2];
for(int index=0;index
在堆栈溢出上搜索“parse hex c#byte array”时,找到了几个匹配项,例如我得到了string.length/2以获得数组中的字节数。但我的问题是如何将其放入数组中。我找到了一些示例,例如空格后面有一个slip“”是的,但不是那种分裂。@Jon首先谢谢你,但是我怎么能不以“0x”开头就做到这一点呢?我对c#不太熟悉。@Noli:你对答案读得有多透彻,你花了多少时间试图理解代码?我只是不明白“parsnyble”是什么以及一些基本的sytex理解“int offset=hex.StartsWith(“0x”)?2:0;“抱歉花了点时间。我的代码现在是PacketS=”70340a1000000000000“byte[]outStream=biginger.Parse(PacketS,NumberStyles.HexNumber).ToByteArray();serverStream.Write(outStream,0,outStream.Length);outStream现在是-outStream{byte[11]}byte[][0]0字节[1]0字节[2]0字节[3]0字节[4]0字节[5]0字节[6]0字节[7]1字节[8]10字节[9]52字节[10]112字节,而不是70,34,0A,01,00,00,00,00,00,00,00,00,00首先反转字节数组,然后写入流:byte[]myBytes=biginger.Parse(“70340A010000000000000”,NumberStyles.HexNumber)。ToByteArray();Array.Reverse(myBytes);myStram.write(myBytes,0,myBytes.Length);BigIntenger不工作,它显示正确的值,但按数组顺序更改。
public static byte[] ConvertHexStringToByteArray(string hexString)
        {
            if (hexString.Length % 2 != 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
            }

            byte[] HexAsBytes = new byte[hexString.Length / 2];
            for (int index = 0; index < HexAsBytes.Length; index++)
            {
                string byteValue = hexString.Substring(index * 2, 2);
                HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
            }

            return HexAsBytes;