簡體   English   中英

如何轉換存儲在字符串中的十六進制並將其除以,並將其存儲在字節數組中

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

我有以下字符串:

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"

然后將其在','字符上拆分為一個數組:

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

現在,我需要一種將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}

您可以使用Convert.ToByte(string,int)通過將16作為第二個參數來將單個字符串從十六進制轉換為二進制。

有了這些知識,再加上一些修整和子字符串處理,我們就可以使用一些LINQ轉換為字節數組:

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

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();

}

遵循同樣的,你會得到你的答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM