0

Well, I got my HexString (PacketS) for example "70340A0100000000000000" I want to split every time after 2 chars and put it into an byte array (Stream).

Means {70, 34, 0A, 01, 00, 00, 00, 00, 00, 00, 00}

Noli
  • 604
  • 2
  • 10
  • 27
  • Searching for "parse hex c# byte array" on Stack Overflow found several hits, such as [this one](http://stackoverflow.com/questions/854012/how-to-convert-hex-to-a-byte-array) – Jon Skeet Jun 13 '12 at 10:34
  • I got the string.length/2 to get the count of bytes in the array. But my problem is how to put that into an array. I found examples where for example a slipt after a space " " is, but noch that kind of split. – Noli Jun 13 '12 at 10:36
  • @Jon Thank you first, but how can I do this wothout the "0x" begining? I am not that familiar with c#. – Noli Jun 13 '12 at 10:43
  • 1
    @Noli: How thoroughly did you read the answer, and how much time did you spend trying to understand the code? – Jon Skeet Jun 13 '12 at 10:45
  • I just do not understand what "ParseNybble" does and some basic sytex understanding like "int offset = hex.StartsWith("0x") ? 2 : 0;" – Noli Jun 13 '12 at 10:51

2 Answers2

1

The shortest path (.NET 4+) is (depending the length or origin):

byte[] myBytes = BigInteger.Parse("70340A0100000000000000", NumberStyles.HexNumber).ToByteArray();
Array.Reverse(myBytes);
myStram.write(myBytes, 0, myBytes.Length);

For previous versions string.length/2 also defines the length of a byte array than can be filled for each parsed pair. This byte array can be written on stream as above.

For both cases, if your origin string is too long, and you want to avoid a huge byte array, proceed with steps getting groups of N characters from your origin.

  • Sorry it took a bit longer. My code is now PacketS = "70340A0100000000000000" byte[] outStream = BigInteger.Parse(PacketS,NumberStyles.HexNumber).ToByteArray(); serverStream.Write(outStream, 0, outStream.Length); outStream is now - outStream {byte[11]} byte[] [0] 0 byte [1] 0 byte [2] 0 byte [3] 0 byte [4] 0 byte [5] 0 byte [6] 0 byte [7] 1 byte [8] 10 byte [9] 52 byte [10] 112 byte instead of 70, 34, 0A, 01, 00, 00, 00, 00, 00, 00, 00 – Noli Jun 13 '12 at 12:39
  • reverse the byte array first and then write to to the stream: byte[] myBytes = BigInteger.Parse("70340A0100000000000000", NumberStyles.HexNumber).ToByteArray(); Array.Reverse(myBytes); myStram.write(myBytes, 0, myBytes.Length); –  Jun 13 '12 at 16:30
  • BigIntenger is not working, it is showing the right values but changed in the array order. – Noli Jun 13 '12 at 16:35
0

This actually worked perfect! I am sorry if your code does the same but I just do not understand.

public static byte[] ConvertHexStringToByteArray(string hexString)
        {
            if (hexString.Length % 2 != 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
            }

            byte[] HexAsBytes = new byte[hexString.Length / 2];
            for (int index = 0; index < HexAsBytes.Length; index++)
            {
                string byteValue = hexString.Substring(index * 2, 2);
                HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
            }

            return HexAsBytes;
Noli
  • 604
  • 2
  • 10
  • 27