c # Hex conversion

1

I need to create an array of bytes with the following content:

byte[] arr = new byte { 0x4f, 0x4e };

I happen to have the string "ON", where "O" = 4f in hex and "N" = 4e

I have tried to store the hex value directly in the byte array without 0x , but when I send it to the serial port in this way, it does not work, it has to be in 0x00 format.

How to convert the string to an array in hexadecimal?

    
asked by anonymous 27.04.2016 / 17:42

1 answer

2

You can use the class Encoding , which has methods to convert from strings to bytes. In your case, for example, you can use Encoding.ASCII :

byte[] arr = Encoding.ASCII.GetBytes("ON");
    
27.04.2016 / 17:48