Hex string to serial port

I want to send a hex string in the form of…

“55AA000280080824802040011101000000000” to the serial port to test some software. I see that serial.write requires the 0x55, etc form.

Is there a library that can do this, or will I need to create a program to parse and manipulate that string to the serial.write form?

Can you please clarify the data type of what you want to send ? From your question it would seem that you want to sends string of characters so why not just print it ?

Sure; I have an system (EFI ECM) and companion PC application that sends out a serial stream to a pc USB port where the application picks up the USB serial stream and displays the received data as gauges. I want to set up an arduino to send out a stream for testing a bluetooth and com0com/hub4com setup. So I need the serial bits of the hex values sent out via the USB connection of the arduino and have them picked up by the application. The application converts the serial bit stream to decimal values and displays their decimal values on the gauge(s).

I can’t make any sense of that

If the EFI ECM sends out a stream of characters such as you quoted, then why can’t you just use


on the Arduino ?

As I understand it, you have this ‘test’ string in HEX to send.
You need to
a) convert each pair of the hex chars to a single byte and then
b) write( ) that byte to the Serial connection

Below is an example sketch.
with TESTING not comment out it converts the hex string to bytes and prints the binary version.
//#define TESTING commented out
it send the binary to the Serial output.
Here is some sample output

 testOutput is  sfTest cap:37 len:37 '55AA000280080824802040011101000000000'
 testOutput does not have even numbers of chars. Ignoring last char!!
01010101 10101010 00000000 00000010 10000000 00001000 00001000 00100100 10000000 00100000 01000000 00000001 00010001 00000001 00000000 00000000 00000000 00000000 

Here is the sketch

// download SafeString library from Arduino library manager
// or from the tutorial page
// https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
#include "SafeString.h"

// uncomment this define to send test output to Serial in char form
#define TESTING

char testOutput[] = "55AA000280080824802040011101000000000";
//char testOutput[] = "05AA";

void printBinToDebug(uint8_t b) {
  cSF(sfBin, 8); // 8 bin digits
  sfBin.print(b, BIN);
  while (!sfBin.isFull()) {

void setup() {
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
#ifdef TESTING
  SafeString::setOutput(Serial); // commenting this out removes error msgs and debug printing

  // wrap the testOutput in a SafeString from processing
  cSFA(sfTest, testOutput); // or createSafeStringFromArray(sfTest,testOutput);  cSFA is just shorthand
  sfTest.debug(F(" testOutput is "));

  if ((sfTest.length() % 2) != 0) {
    SafeString::Output.println(F(" testOutput does not have even numbers of chars. Ignoring last char!!"));

  cSF(sf2hex, 2); // as SafeString to hold two hex chars
  size_t startIdx = 0;
  size_t endIdx = startIdx + 2;
  long hexValue;
  while (endIdx <= sfTest.length()) {
    sfTest.substring(sf2hex, startIdx, endIdx);
    if (sf2hex.hexToLong(hexValue)) {
      // valid hex
#ifdef TESTING
      printBinToDebug(hexValue); SafeString::Output.print(' ');// for testing
      Serial.write((uint8_t)hexValue); // send as unsigned 8 bits  <<<<<< this is the real data when not TESTING
    } else {
      //invalid skip this one
      SafeString::Output.print(sf2hex); SafeString::Output.println(F(" is not valid hex"));
    startIdx = endIdx;
    endIdx += 2; // next pair

void loop() {

OK… sorry. I’ll try again.

I understand from this post that to send a hex value out of the serial/usb port, you need to use the form “Serial.write(0xAA);”, one byte at a time or as an array. I have 273 bytes to send in the form shown above, so I’d have to re-code the string with commas and the ‘0x’ string. I’d like to find a library that I can call to do this for me by sending it the raw hex string.

I guess I could have done manually it in the time it took to explain it, but I’m looking for a general answer. Sorry.

The function that you’re looking for is strtoul. You take two characters of your text, put them in a 3 character array and convert with strtoul. Take the next two characters and repeat.

Serial.write() sends the raw value of the byte, so if you use
Serial.write(0x45); then 69 (decimal) will be sent. The 0x is only necessary if what you have is in hex

Given “55AA000280080824802040011101000000000” please confirm that you want to send 0x55 then 0xAA then 0x00 etc ?

I would just copy ‘, 0x’ into the editor clipboard and type Right-Arrow, Right-Arrow, Paste over and over. It’s doesn’t take that long.

const byte DataStream[] = 
  0x55, 0xAA, 0x00, 0x02, 0x80, 0x08, 0x08, 0x24, 0x80, 
  0x20, 0x40, 0x01, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 
const int DataStreamLength = sizeof DataStream / sizeof DataStream[0];

  Serial.write(DataStream, DataStreamLength);

Yes, I want to send out that hex string as hex. So ‘5’ will go out as binary 0101 for example, rather than as ASCII binary 00110101.

All I can say is… WOW!
and thank you.

‘5’ cannot go out as 0101 unless accompanied by 4 more bits, so I presume that “51” should go out as 01010001 or perhaps you mean that it should go out as 00000101 followed by 00000001, which is unlikely but possible.

a little bit nibble arithmetic and knowledge about American Standard Code for Information Interchange should help :nerd_face:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.