0

I need to convert a string array to a byte array. I know this question has been posted and answered many times, but for some reason, none of the solutions work for me.

Here is the most common solution:

public static byte[] StringArrayToByteArray(string[] someStringArray)
{
    return someStringArray.Select(s => Convert.ToByte(s, 16)).ToArray();
}

However, when I run this, I get the following errors: FormatException: Could not find any parsable digits. or FormatException: Additional unparsable characters are at the end of the string.

I need to save letters, along with a separator (/), for my data. Here is my code:

        string[] stringArray = new string[map.Size.x * map.Size.y];
        int counter = 0;
        for (int y = (int) map.Rect.y; y < map.Rect.yMax; y++)
        {
            for (int x = (int) map.Rect.x; x < map.Rect.xMax; x++)
            {
                stringArray[counter] = map[x,y].Name;
                stringArray[counter] += "/";
                counter++;
            }
        }

Then I need to convert this string[] to a byte array.

  • Does this answer your question? [How to Convert String to Byte Array?](https://stackoverflow.com/questions/30545162/how-to-convert-string-to-byte-array) and [Converting string to byte array in C#](https://stackoverflow.com/questions/16072709/converting-string-to-byte-array-in-c-sharp) and [Convert String to ByteArray in C#](https://stackoverflow.com/questions/44968961/convert-string-to-bytearray-in-c-sharp) and [Convert String\[\] to byte\[\] array](https://stackoverflow.com/questions/10531148/convert-string-to-byte-array) –  Jun 05 '21 at 14:19
  • No. Appending data to an existing string is extremely slow in my case. I need to be able to convert a string array to a byte array, fast. –  Jun 05 '21 at 14:21

2 Answers2

0

You can combine those strings to a single string and return its byte[]. ie:

public static byte[] StringArrayToByteArray(string[] someStringArray)
{
    return string.Join("",someStringArray).Select(x => (byte)x).ToArray();
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • Simple, and works surprisingly fast. Thank you! –  Jun 05 '21 at 14:29
  • @OlivierRogier Thank you for the suggestions. I didn't think to join the entire array first and then perform the conversion. –  Jun 05 '21 at 14:31
0

The fastest way (performance-wise) to do this is to use one of the overloads of Encoding.GetBytes which allows for a supplied byte[] buffer.

This is only going to work with an encoding which is fixed width, either Unicode or ASCII. It's not clear which encoding you want. The reason you got that error is because of characters that won't fit in a byte, so I've gone with Unicode.

public static byte[] StringArrayToByteArray(string[] someStringArray)
{
    var buffer = new byte[someStringArray.Sum(s => s.Length) * 2];   // remove * 2 for ASCII
    var position = 0;
    foreach (var s in someStringArray)
        position += Encoding.Unicode.GetBytes(s, o, s.Length, buffer, position);

    return buffer;
}

You can also use the new Span<byte> overload in newer versions of .NET

Charlieface
  • 52,284
  • 6
  • 19
  • 43