1

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;
        }
6
  • 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. Commented Jan 8, 2020 at 9:45
  • 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....}. Commented Jan 8, 2020 at 9:53
  • 1
    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... Commented Jan 8, 2020 at 10:13
  • 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. Commented Jan 8, 2020 at 10:18
  • 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); Commented Jan 8, 2020 at 10:20

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

I need the bytes to be in hex format not the string, have a look at the comment i put above for what i mean
I just converted back to a string so you can see the results. You would send data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.