1
votes

Bit of a weird one here but I'm trying to write a control program for a monitor that uses a serial RS232 port to receive commands in hexadecimal, 0xFF, notation i.e. 0x00.

Problem I have is that I can't seem to find a way of taking a user input, which is a decimal value like 55, and converting it into its byte, hexadecimal, form with the above mentioned format. Using the Convert method that Visual studio has built in gives me the correct value i need but without the required 0x at the beginning.

I'm new to using bytes and byte arrays in C# so forgive me if I've missed out on a simple formatting method that will solve it.

Below is a string to byte array method that I found on here and its helpful but gives me the wrong format for the bytes.

private 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, 2), 16);
            return bytes;
        }
2
You code converts a hex string (without the prefix 0x) to it's encoded byte values. If you want it to support strings like "0x55" you need to add if (hex.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { hex = hex.Substring(2); } at the beginning, which will remove the prefix (if extisting) so that the existing code can process the hexstring.ckuri
Thank you for the help, would you have any idea how to convert the full 0x55 to a byte of {0x55}? That's where the main problem is. The byte array should look like {0xA6,0x01,0x00....}.Mac Man
You can't add 0x to a byte, only to a string where your binary data might be represented as hex data e.g. $"0x{bytes[0]}0x{bytes[1]}0x{bytes[2]}". Does the monitor require 0xA6 as hex formatted string or as byte? You may assume 0x to be in front of the byte, but that is only for human readabiltiy of binary data. If you right click in the watch window of your debugger session you could enable Hexadecimal Display then you will see 0x in front of your byte variables as well...MarkusEgle
the monitor requires the bytes to be in the format of 0xA6, i tested it sending just the A6,01,00..... and it didnt recognise the command. Why i don't know.Mac Man
this is the code that i used to test the control of the monitor and it worked. byte[] temp2 = { 0xA6, 0x01, 0x00, 0x00, 0x00, 0x04, 0x01, 0xC0, 0x50, 0x32 }; MonPort.Write(temp2, 0, temp2.Length);Mac Man

2 Answers

1
votes

You might just want to convert a hex-string to bytes. See How do you convert a byte array to a hexadecimal string, and vice versa?

simple conversion of one single "hex-string-byte" and send the byte:

MonPort.Write(Convert.ToByte("A6"), 0,1);

Convert content of your textbox to a binary array and send it:

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;
}


public void SendData()
{
    byte[] sendBytes = StringToByteArray(YourSendBytesTextbox.Text);
    MonPort.Write(sendBytes, 0, sendBytes.Length);
}
0
votes

See following :

            string input = "abcdefghijk";
            byte[] data = string.Join(",",input.AsEnumerable()
                .Select(x => "0x" + ((byte)x).ToString("X2")))
                .Select(x => (byte)x).ToArray();
            string str = Encoding.UTF8.GetString(data);