0

I have a string containing hexadecimal data and I would like to append it to a byte array previously defined. How can I do this?

// this is my string
string s = "7C04048404048C04049404059C0405";

// this is my byte array to which I want to append the string, keeping the same format of the array
bufferByte = new byte[] { 0x40, 0x03, 0xE8, 0x03, 0xE8, 0x49}

Up to now, this is what I have tried:

byte[] coordinate = StringToByteArray2(s);

Where the function I have used is:

        public static byte[] StringToByteArray2(String hex)
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;
        }

The problem is that in this way I'm obtaining an array in dec, while I wish to obtain it in the hex form, as specified above. Any help is appreciated! Thank you in advance!

lizzi
  • 13
  • 2
  • 2
    `The problem is that in this way I'm obtaining an array in dec` No, you don't. Both of these arrays will be stored in binary in memory. – tkausl Mar 22 '22 at 09:23
  • You can't append to an existing array - you would have to resize an existing one and copy over the extra elements, or create an entirely new one and copy the original and then the extra elements. – Matthew Watson Mar 22 '22 at 09:24
  • @tkausl my bytes need to be interpreted, so the format IS important... – lizzi Mar 22 '22 at 09:36
  • @lizzi Which version of .net are you using? The answer depends on that. – Matthew Watson Mar 22 '22 at 09:38
  • @MatthewWatson 4.8.04084, I think – lizzi Mar 22 '22 at 09:50
  • 1
    Not sure what you mean with "my bytes need to be interpreted", a byte is a byte. It has not format. Or do you mean that the source code will will be interpreted? I.e. Do you want to convert a hex-string to the corresponding source code for declaring an array? – JonasH Mar 22 '22 at 09:55

1 Answers1

0

If you have an existing byte[] array, you can't change the size of it.

You can create a new array of a different size using Array.Resize() (this changes the array's reference to reference a new one) or you can create a new array of the correct size and copy the prefix bytes and the appended bytes into it.

This is very easy with Linq if you're using .NET 5.0 or later:

bufferByte = bufferByte.Concat(Convert.FromHexString(s)).ToArray();

Full sample:

using System;
using System.Linq;

static class Program
{
    public static void Main()
    {
        // this is my string
        string s = "7C04048404048C04049404059C0405";

        // this is my byte array to which I want to append the string, keeping the same format of the array
        var bufferByte = new byte[] { 0x40, 0x03, 0xE8, 0x03, 0xE8, 0x49 };

        bufferByte = bufferByte.Concat(Convert.FromHexString(s)).ToArray();

        Console.WriteLine(Convert.ToHexString(bufferByte)); // 4003E803E8497C04048404048C04049404059C0405
    }     
}

Try it online: https://dotnetfiddle.net/9v54EE

If you're using an earlier version of .NET you'll have to write your own converter for the hex string:

using System;
using System.Linq;

public static class Program
{
    public static void Main()
    {
        // this is my string
        string s = "7C04048404048C04049404059C0405";

        // this is my byte array to which I want to append the string, keeping the same format of the array
        var bufferByte = new byte[] { 0x40, 0x03, 0xE8, 0x03, 0xE8, 0x49 };

        bufferByte = bufferByte.Concat(StringToByteArray(s)).ToArray();

        Console.WriteLine(ByteArrayToString(bufferByte)); // 4003E803E8497C04048404048C04049404059C0405
    }

    public static string ByteArrayToString(byte[] ba)
    {
        return BitConverter.ToString(ba).Replace("-", "");
    }

    public static byte[] StringToByteArray(string hex)
    {
        int    numberChars = hex.Length;
        byte[] bytes       = new byte[numberChars / 2];

        for (int i = 0; i < numberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);

        return bytes;
    }
}

Try it online: https://dotnetfiddle.net/vKCHYs

I copied the conversion code from here: https://stackoverflow.com/a/311179/106159

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276