hex2ascii

Hi,

I need convert hex as ascii (string)

like hex=df ->> string df

Thank You for help

http://www.cplusplus.com/reference/cstdlib/itoa/

Thank you but i have still a problem...

++++
String datahex = "";
char data;
...
data = Serial.read();
datahex = String(data,HEX);
...
++++

how change to 'char' datahex as ascii ?

why? i need write hex as string

Your problem, most likely, is that you think you want to use the String class.
That may well not be the case.

how change to 'char' datahex as ascii ?

What value do you think is in datahex? What value are you wanting to see? How and where?

I'm with AWOL, drop the String class...

Wat?

Are you saying that you have a string containing the hexadecimal representation of a number, and you need to convert that into a string containing the decimal representation?

Are you saying that you used a hexadecimal numeric constant in some code, and you want to print out the decimal representation of that constant?

Are you saying that you used a hexadecimal numeric constant in some code, and you want to print out the hexadecimal representation of it?

I think it’s the last one. Do you want leading zeros or not? I’ll assume you do - it’s easier that way. Did you want the result in a string or did you just want it printed to serial? Is lowercase ok? Did you want the trailing null terminator added? Shall we assume that an int is 16 bits (4 digits)? Shall we ignore sign and treat everything as unsigned? Did you want the leading “0x” put into the string?

What I’m saying, is that yo have left quite a bit unspecified. If you specified it, you might even be able to write it yourself.

// make sure that s points to an array of char at least 5 bytes long.
void intToHexString(unsigned int num, char *s) {
  for(int i = 12; i>=0; i-=4) {
    int digit = (num >> i) & 0xF;
    *s++ = digit + (digit < 10 ? '0' : 'a'-10);
  }
  *s = '\0';
}

And if you want leading zeros ignored, it’s:

// make sure that s points to an array of char at least 5 bytes long.
void intToHexString(unsigned int num, char *s) {
  if(num ==0) {
    *s++ = '0';
  }
  else {
    while((num & 0xF000) == 0)
      num <<= 4;
    while(num != 0) {
      int digit = (num >> 12) & 0xF;
      *s++ = digit + (digit < 10 ? '0' : 'a'-10);
      num <<= 4;
    }
  }
  *s = '\0';
}