如何将字符串转换为无符号int 32 C#的byte []

Mir*_*roo 4 c# bytearray

我有一个字符串"0x5D, 0x50, 0x68, 0xBE, 0xC9, 0xB3, 0x84, 0xFF".我想将其转换为:

byte[] key= new byte[] { 0x5D, 0x50, 0x68, 0xBE, 0xC9, 0xB3, 0x84, 0xFF};
Run Code Online (Sandbox Code Playgroud)

我考虑过将字符串拆分,然后循环它并将setvalue转换byte[]为索引中的另一个i

string Key = "0x5D, 0x50, 0x68, 0xBE, 0xC9, 0xB3, 0x84, 0xFF";

    string[] arr = Key.Split(',');
    byte[] keybyte= new byte[8];
    for (int i = 0; i < arr.Length; i++)
    {
         keybyte.SetValue(Int32.Parse(arr[i].ToString()), i);
    }
Run Code Online (Sandbox Code Playgroud)

但似乎它不起作用.我在第一个开头将字符串转换为unsigned int32时遇到错误.

任何帮助,将不胜感激

Guf*_*ffa 5

你可以这样做:

byte[] data =
  Key
  .Split(new string[]{", "}, StringSplitOptions.None)
  .Select(s => Byte.Parse(s.Substring(2), NumberStyles.HexNumber))
  .ToArray();
Run Code Online (Sandbox Code Playgroud)