Array of hex to char string

Status
Not open for further replies.

MolsonB

Active member
I'm going bald over this. I know it's right there, but I've searched and played around for hours.

I have ....
Code:
uint8_t hexData[6];
data[0] = 0x5B;
data[1] = 0x01;
data[2] = 0xFF;
data[3] = 0x00;
data[4] = 0x0F;

How do I get it to ?
Code:
 char[] charData = "5B01FF00F"

I've tried adding ..... and it prints the symbols. Looking for it to print the actual string above. Since my original array has 0x00 in the middle, messes things up.
Code:
data[5] = '\0'
Serial.println((char *)VPXData_raw);

I made a function that loops through the hexData array and builds a String (capital S), but I'm trying to do this sticking to char array.
 
Sample as shown uses no consistent variable so nothing above would compile**? :: hexData, data[], charData, VPXData_raw

This would get the hex data into an array:
Code:
char charData[6] = { 0x5B, 0x01, 0xFF, 0x00, 0x0F, 0x00 }

Serial.println( charData ); // should print anything up to the NULL 4th character. Except I'm not sure what the non-printable 2nd char 0x01 will do

**Forum Rule: Always post complete source code & details to reproduce any issue!
 
I am confused, are you wanting it to print the actual hex characters, or do you wish for it to print the Ascii representation for it? I am assuming the later as that is the only way that println makes sense.

If you wish to write the actual hex stuff out, you could do something like: Serial.write(charData, sizeof(charData));

If you wish to print out the ASCII versions, you can do it many ways, but one way is to do something like:
Code:
for (int i=0; i < sizeof(charData); i++) 
{
  Serial.print(charData[i], HEX);
}
Note: there will probably still be issues, like printing things like 0 will only output one digit. If you want multiple there are again are options like:
Code:
for (int i=0; i < sizeof(charData); i++) 
{
  if (chardata[i] < 16)
    Serial.print("0");
  Serial.print(charData[i], HEX);
}
Warning all of this typed on the fly, so could be issues...
 
Yes, my example was typed on the fly as well. Was not a full sketch. I wanted to take the actual hex value array (0x35, 0x01), save it in a char string. (3501 aka 0x33, 0x35, 0x30, 0x31).

I've created a full sketch now to test things out. Created a function that loops through each byte and converted it to 2 char spaces. Also one that reverses it. Wasn't sure if there was a simple command to do the functions I've came up with.

Code:
uint8_t hexData[5] = {0x35, 0x01, 0xFF, 0x00, 0x0F};
char charData[] = "3501FF000F";
char mycharData[(sizeof(hexData)*2)+1]; //Take hex length, times 2 (each byte is split into 2 char spots), add 1 (ending null)
uint8_t myhexData[(sizeof(charData)/2)-1];


void setup() {
  Serial.begin(57600);
}

void loop() {
  Serial.println(String("Hex:")+ (char *)hexData);
  Serial.println(String("Char:")+ charData);

  //Convert byte array to string of the actual HEX values (0xFF, 0x3A = FF3A) (double in size)
  HexToChar(hexData, sizeof(hexData), mycharData);
  Serial.println(String("myChar:")+ mycharData);

  //Converts string of the actual HEX values to byte array (FF3A = 0xFF, 0x3A) (half the size)  
  uint16_t len = CharToHex(charData, sizeof(charData), myhexData);
  Serial.print("myHex:");
  for (uint16_t i=0; i < len; i++) {
  if (myhexData[i] < 0x10){Serial.print("0");}
    Serial.print(myhexData[i],HEX);
  }
  Serial.println();

  delay(5000);
}

//Convert a byte array to char of the actual HEX numbers (double in size)
void HexToChar (uint8_t in[], uint16_t len, char out[]) {
  uint8_t lowbyte, highbyte;
  uint16_t pos = 0; 
  
  for (uint16_t i=0; i < len; i++) { 
    highbyte = in[i] >> 4;
    lowbyte = in[i] & 0xF;
    pos = (i*2);

    if (highbyte >= 0x00 && highbyte <= 0x09)
      out[pos] = highbyte + '0';
    else
      out[pos] = highbyte + 'A' - 10;

    if (lowbyte >= 0x00 && lowbyte <= 0x09)
      out[pos+1] = lowbyte + '0';
    else
      out[pos+1] = lowbyte + 'A' - 10;
  }  
  out[pos+2] = '\0';    //End of char's
}

//Converts string of the actual HEX values to byte array (FF3A = 0xFF, 0x3A) (half the size)  
uint16_t CharToHex(char *in, uint16_t len, uint8_t *out) {
  uint16_t x = 0;
  uint8_t c, h8[2];
  for (uint16_t i = 0; i < len-1; i += 2) {

    for (uint16_t ii=0; ii<2; ii++) {
      c = in[i+ii];
      if (c <= '9' && c >= '0') {  c -= '0'; }
      else if (c <= 'f' && c >= 'a') { c -= ('a' - 0x0a); }
      else if (c <= 'F' && c >= 'A') { c -= ('A' - 0x0a); }
      else return(-1);
      h8[ii] = c;
    }

    out[x] = h8[0]<<4 | h8[1];
    x++;
  }
  
  return (x);
}
 
I don't understand exactly what do you want.. :)
Something like this ? (converts efficiently a number to a string)

Code:
static const char hexMap[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
uint8_t test = 0x12;

String x;

void setup() {
  // put your setup code here, to run once:
 while(!Serial){;}
 delay(100);

  //convert "test" to a String x:
 x = hexMap[test >> 4];
 x += hexMap[test & 0x0f];

 Serial.println(x);
}

void loop() {}
 
Frank, yes you understand me correct. Your solution is nice and simple. I had to first change from a String to char array to speed things up.
Around 200bytes, both our functions are only a few micros() apart.
 
Status
Not open for further replies.
Back
Top