C#Bytes String to Bytes Array

时间:2022-03-11 21:38:42

I have following string of bytes

我有以下字符串

17 80 41 00 01 00 01 00 08 00 44 61 72 65 46 61 74 65 01 00 00 00 01 00 03 00 01 00 09 00 43 68 61 6E 6E 65 6C 2D 31 00 00 02 00 09 00 43 68 61 6E 6E 65 6C 2D 32 65 00 03 00 09 00 43 68 61 6E 6E 65 6C 2D 33 65 00

17 80 41 00 01 00 01 00 08 00 44 61 72 65 46 61 74 65 01 00 00 00 01 00 03 00 01 00 09 00 43 68 61 6E 6E 65 6C 2D 31 00 00 02 00 09 00 43 68 61 6E 6E 65 6C 2D 32 65 00 03 00 09 00 43 68 61 6E 6E 65 6C 2D 33 65 00

What is the best way to take it as input from user and make it into byte array?

将它作为用户输入并将其转换为字节数组的最佳方法是什么?

3 个解决方案

#1


11  

Try:

尝试:

string text = ...
byte[] bytes = text.Split()
                   .Select(t => byte.Parse(t, NumberStyles.AllowHexSpecifier))
                   .ToArray();

If you want to only split on the space-character (rather than any whitespace) use Split (' ').

如果您只想拆分空格字符(而不是任何空格),请使用拆分('')。

#2


7  

If the user is inputting it into the command line just like that, do this:

如果用户将其输入命令行,请执行以下操作:

        string input = GetInput(); // this is where you get the input
        string[] numbs = input.Split(' ');
        byte[] array = new byte[numbs.Length];
        int i = 0;

        foreach (var numb in numbs)
        {
            array[i++] = byte.Parse(numb, NumberStyles.HexNumber);
        }

#3


1  

You can use the Parse method in System.Byte to parse the individual hax pairs:

您可以使用System.Byte中的Parse方法来解析单个hax对:

// Get the string from the user
string s=Console.ReadLine();

// Convert to a byte array
byte[] sBytes=s.Split(new char[] {' '})
               .Select(hexChar => byte.Parse(hexChar,NumberStyles.HexNumber))
               .ToArray();

// *** Test code follows ***

// Display the bytes (optional), to verify that the conversion worked
StringBuilder hexString=new StringBuilder(sBytes.Length*3);

foreach (byte b in sBytes)     
{
  // Separate hex pairs with a space
  if (hexString.Length>0)
    hexString.Append(' ');
  // Append next hex pair (i.e., formatted byte)
  hexString.AppendFormat("{0:x2}",b);
}

Console.WriteLine(hexString);

#1


11  

Try:

尝试:

string text = ...
byte[] bytes = text.Split()
                   .Select(t => byte.Parse(t, NumberStyles.AllowHexSpecifier))
                   .ToArray();

If you want to only split on the space-character (rather than any whitespace) use Split (' ').

如果您只想拆分空格字符(而不是任何空格),请使用拆分('')。

#2


7  

If the user is inputting it into the command line just like that, do this:

如果用户将其输入命令行,请执行以下操作:

        string input = GetInput(); // this is where you get the input
        string[] numbs = input.Split(' ');
        byte[] array = new byte[numbs.Length];
        int i = 0;

        foreach (var numb in numbs)
        {
            array[i++] = byte.Parse(numb, NumberStyles.HexNumber);
        }

#3


1  

You can use the Parse method in System.Byte to parse the individual hax pairs:

您可以使用System.Byte中的Parse方法来解析单个hax对:

// Get the string from the user
string s=Console.ReadLine();

// Convert to a byte array
byte[] sBytes=s.Split(new char[] {' '})
               .Select(hexChar => byte.Parse(hexChar,NumberStyles.HexNumber))
               .ToArray();

// *** Test code follows ***

// Display the bytes (optional), to verify that the conversion worked
StringBuilder hexString=new StringBuilder(sBytes.Length*3);

foreach (byte b in sBytes)     
{
  // Separate hex pairs with a space
  if (hexString.Length>0)
    hexString.Append(' ');
  // Append next hex pair (i.e., formatted byte)
  hexString.AppendFormat("{0:x2}",b);
}

Console.WriteLine(hexString);