简体   繁体   English

如何转换存储在字符串中的十六进制并将其除以,并将其存储在字节数组中

[英]How do i convert hexadecimal that is stored in string and split it by , and store it in a byte array

I have the following string: 我有以下字符串:

string hi = "0xfc,0xe8,0x82,0x00,0x00,0x00,0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,
0x8b,0x52,0x0c,0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,0xe2,0xf2,0x52,
0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,0x11,0x78,0xe3,0x48,0x01,0xd1,
0x51,0x8b,0x59,0x20,0x01,0xd3,0x8b"

And I split it on the ',' character into an array: 然后将其在','字符上拆分为一个数组:

string[] string1 = decrypted.Split(',');

Now I need a way to store string1 into a byte array so it looks like: 现在,我需要一种将string1存储到byte数组中的方式,如下所示:

byte[] byte1 = {0xfc,0xe8,0x82,0x00,0x00,0x00,0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,
0x8b,0x52,0x0c,0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,0xe2,0xf2,0x52,
0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,0x11,0x78,0xe3,0x48,0x01,0xd1,
0x51,0x8b,0x59,0x20,0x01,0xd3,0x8b}

You can convert a single string from hex to binary using Convert.ToByte(string, int) by passing 16 as the second parameter. 您可以使用Convert.ToByte(string,int)通过将16作为第二个参数来将单个字符串从十六进制转换为二进制。

With this knowledge, and a little trimming and substringing, we can convert to an array of bytes with a little LINQ: 有了这些知识,再加上一些修整和子字符串处理,我们就可以使用一些LINQ转换为字节数组:

var byteArray = input
    .Split(',')
    .Select
    ( 
        s => Convert.ToByte
        (
            s.Trim().Substring(2),
            16
        ) 
    )
    .ToArray();

Example on DotNetFiddle DotNetFiddle上的示例

public static byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
                 .Where(x => x % 2 == 0)
                 .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                 .ToArray();

} }

follow the same you will get your answer 遵循同样的,你会得到你的答案

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

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