0

how can I create a byte data type from a string? For example: The device I am sending data to, expects the data to be in the hexadecimal format. More specifically, it needs to be in the format: 0x{hexa_decimal_value}

Hard coded, it already worked sending data this way. I would create a byte array like this:

byte[] items_to_send_ = new byte[] {0x46, 0x30, 0x00};

Now I want to code it dynamically.

The code I am now trying to write looks like this:

var ListByte = new List<byte>();

foreach (char val in messageToConvert)
{

    var hexa_decimal_val = Convert.ToInt32(val).ToString("X");
    hexa_decimal_val = $"0x:{hexa_decimal_val}";

    byte val_ = CreateByteFromStringFunction(hexa_decimal_val); // How? 


    ListByte.Add(val_); 

}

The step in question is when creating the variable val_, where I want to build the byte value from hexa_decimal_val, but I just don't know how. Casting does not work, and I did not find any other function that would do it for me.

It feels like there should be a really easy solution to this, but I just don't seem to find it.
What makes looking for the correct answer tricky here is that I already know how to convert from string to hexadecimal value, but the conversion afterwards is nowhere to be found.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
david9990
  • 3
  • 2
  • 2
    Have you tried GetBytes() , see this doc [page](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytes?view=net-6.0) – Anand Sowmithiran Oct 31 '22 at 13:46
  • byte[] ListByte = Encoding.ASCII.GetBytes(messageToConvert); – Kroepniek Oct 31 '22 at 13:46
  • 1
    Does this answer your question? [Converting string to byte array in C#](https://stackoverflow.com/questions/16072709/converting-string-to-byte-array-in-c-sharp) – Julian Oct 31 '22 at 13:47
  • `Convert.ToInt32` converts a number in a string to an int. E.g., string `"123"` becomes int `123`. – Olivier Jacot-Descombes Oct 31 '22 at 13:49
  • The real question is why are you converting an `char` to an `Int32`, to a `String` and then to a `byte`? Why not just convert the `char` to a `byte`? – Heretic Monkey Oct 31 '22 at 13:49
  • Bytes have no format. They're binary values. `hexadecimal format` that's a *string* format that requires multiple characters to represent the original byte. If you convert that string back into a `byte` you'll get the original value back. What does that device *really* need? Bytes? Or sending 4 bytes representing that integer when just 1 would do? What does `messageToConvert` contain? – Panagiotis Kanavos Oct 31 '22 at 13:51
  • You can convert strings to bytes using `Encoding.GetBytes` provided you know what encoding is used by the device. For the 7-bit US-ASCII range every encoding would do. Even English text contains characters beyond that range though. If the device works with UTF8 you can use `Encoding.UTF8.GetBytes`. If it uses Latin1, `Encoding.Latin1.GetBytes()`. For other codepages you can use [Encoding.GetEncoding(codepage)](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding?view=net-7.0) – Panagiotis Kanavos Oct 31 '22 at 13:58
  • Try following : string output = string.Join(" ", items_to_send_.Select(x => "0x" + x.ToString("x2"))); – jdweng Oct 31 '22 at 14:21

1 Answers1

0

You don't need to do create bytes from characters one by one and append to a list. You can usethis;

var encodedByteList = Encoding.UTF8.GetBytes(messageToConvert);

If you still want to do that, you can do something like this;

var encodedByteList = new List<byte>();

foreach (var character in messageToConvert)
{
    var correspondingByte = (byte)character;

    encodedByteList.Add(correspondingByte);
}

Or with LINQ, you can use this one liner;

var encodedByteList = messageToConvert.Select(c => (byte)c);
cemahseri
  • 397
  • 2
  • 12
  • Thanks, totally overlooked that the 0xXX format was simply another representation. What I did not mention is that I need to add a termination character at the end. I simply do this now by converting to a List and then back to an array – david9990 Oct 31 '22 at 14:44