CSharp · Console
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;
}
}