Converting HEX to ASCII

Hi all! I have an RFID reader outputting a string of hex digits that are printed to the Serial monitor (as hex digits). I want them to be decoded to be their corresponding ASCII characters instead. For example, I will get something like epc[54 65 73 74 4D 63 4B 65 6C 6C 61 72] (running great) and that will be printed to the serial monitor. What I would like to do is take that hex code and translate it to [running great] and print that.
Note: I am using the SparkFun Simultaneous RFID reader (and some modified code from its examples)
My code (printing the hex digits):

 if(nano.check() == true){
    byte responseType = nano.parseResponse();
    if(responseType == RESPONSE_IS_TAGFOUND){
      Serial.print(F(" epc["));
      byte tagEPCBytes = nano.getTagEPCBytes();
      for(byte x = 0 ; x< tagEPCBytes ; x++){
        if(nano.msg[31 + x] < 0x10) Serial.print("0");
        Serial.print(nano.msg[31 + x], HEX);
        Serial.print((" ")); 
      }
      Serial.print("]");
      Serial.println();
    }
  }

I suggest that you post a minimal verifiable sketch.

Complete sketch:

#include <SoftwareSerial.h>
SoftwareSerial softSerial(2, 3);
#include "SparkFun_UHF_RFID_Reader.h"
RFID nano;
#define buzzer 9
#define buzzergnd 10

void setup() {
  // put your setup code here, to run once:
  pinMode(buzzer, OUTPUT);
  pinMode(buzzergnd, OUTPUT);

  digitalWrite(buzzergnd, LOW);
  Serial.begin(115200);
  while(!Serial);
  if (setupNano(38400) == false) //Configure nano to run at 38400bps
  {
    Serial.println(F("Module failed to respond. Please check wiring."));
    while (1); //Freeze!
  }
  nano.setRegion(REGION_NORTHAMERICA);
  nano.setReadPower(2700);
  Serial.println("Press a key to begin scanning for tags");
  while(!Serial.available());
  Serial.read();
    nano.startReading();


}

void loop() {
  // put your main code here, to run repeatedly:
  if(nano.check() == true){
    byte responseType = nano.parseResponse();
    if(responseType == RESPONSE_IS_TAGFOUND){
      Serial.print(F(" epc["));
      byte tagEPCBytes = nano.getTagEPCBytes();
      for(byte x = 0 ; x< tagEPCBytes ; x++){
        if(nano.msg[31 + x] < 0x10) Serial.print("0");
        Serial.print(nano.msg[31 + x], HEX);
        Serial.print((" "));
        
      }
      Serial.print("]");
      Serial.println();
       
    }
   
  }
}
boolean setupNano(long baudRate)
{
  nano.begin(softSerial); //Tell the library to communicate over software serial port

  //Test to see if we are already connected to a module
  //This would be the case if the Arduino has been reprogrammed and the module has stayed powered
  softSerial.begin(baudRate); //For this test, assume module is already at our desired baud rate
  while (softSerial.isListening() == false); //Wait for port to open

  //About 200ms from power on the module will send its firmware version at 115200. We need to ignore this.
  while (softSerial.available()) softSerial.read();

  nano.getVersion();

  if (nano.msg[0] == ERROR_WRONG_OPCODE_RESPONSE)
  {
    //This happens if the baud rate is correct but the module is doing a ccontinuous read
    nano.stopReading();

    Serial.println(F("Module continuously reading. Asking it to stop..."));

    delay(1500);
  }
  else
  {
    //The module did not respond so assume it's just been powered on and communicating at 115200bps
    softSerial.begin(115200); //Start software serial at 115200

    nano.setBaud(baudRate); //Tell the module to go to the chosen baud rate. Ignore the response msg

    softSerial.begin(baudRate); //Start the software serial port, this time at user's chosen baud rate

    delay(250);
  }

  //Test the connection
  nano.getVersion();
  if (nano.msg[0] != ALL_GOOD) return (false); //Something is not right

  //The M6E has these settings no matter what
  nano.setTagProtocol(); //Set protocol to GEN2

  nano.setAntennaPort(); //Set TX/RX antenna ports to 1

  return (true); //We are ready to rock
}

Is this what you mean?

Serial.print(char(nano.msg[31 + x]));

Just leave out the HEX keyword...

char myString[12] = {0x54, 0x65, 0x73, 0x74,0x4D, 0x63, 0x4B, 0x65, 0x6C, 0x6C, 0x61, 0x72};

void setup() 
{
  Serial.begin (9600);
  delay(200);
  Serial.println();
  
  for (uint8_t x = 0; x < 12; x++)
  {
    Serial.print(myString[x], HEX); 
    Serial.print(" ");
  }

  Serial.println();

  for (uint8_t x = 0; x < 12; x++)
  {
    Serial.print(myString[x]); 
  }
}

void loop() 
{

}

Output

54 65 73 74 4D 63 4B 65 6C 6C 61 72 
TestMcKellar

Thanks for the help! Almost works, prints epc[ ]. Doesn't print the hex digits, but it doesn't print ASCII either.

Is there a way I could change myString to be whatever the RFID reader outputs?

Update: groundFungus's solution worked! I'm not sure why exactly it wasn't before, but re-tried and worked as intended. Thank you!

nano.msg[31 + x] = myString[x]

...assuming it's a char array. Otherwise you'll need char(nano.msg[31 + x]).

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