Converting byte data to hex and then Sending HEX values via Arduino serial

Hello guys.
I’m using the pubsub client library to receive data from Ibm Bluemix server to ESP nodemcu.
In the callback function the payload which is received is stored in the variable “payload” with the datatype byte*.

void callback(char* topic, byte* payload, unsigned int payloadLength) 
{

  for (int i = 0; i < payloadLength; i++)
  {
           
      Serial.print((char)payload[i]);  /// <-----Here, Instead of this, i have to send the payload in hex.
  }
}

Now i want to send that data into hexadecimal format via arduino serial port. I’m aware that by using 0x55 we can send “55” as a hex number. But how can i send the payload that i’m receiving which is in byte format in hex? I’m really getting confused with the datatypes over here. Can someone clear this dilemma?

Thank you.
Nishit.

      Serial.print(payload[i], HEX);

No. I have tried this, i don't want this. Suppose for example

int x = 0x0F;
Serial.write(x);

Here the output that i get is 15 which is F in hexadecimal. Similarly if the byte* payload contains a 'F' it should send 15 as above.

Similarly if the byte* payload contains a 'F' it should send 15 as above.

Then you should use serial.write() instead of serial.print().

"Hex" is not a data type. It is a human readable representation of a binary number.

So you want to convert the ASCII representation of a hexadecimal digit (like ‘F’) to its decimal equivalent (like 15)?

(x >='0' && x <= '9') ? x - '0' : (x >='A' && x <= 'F') ? (x - 'A')+10 : 0

If you want to allow lowercase a-f as well:

(x >='0' && x <= '9') ? x - '0' : (x >='A' && x <= 'F') ? (x - 'A')+10 :  (x >='a' && x <= 'f') ? (x - 'a')+10 : 0

Thanks for the reply. I tried the following code

char x;

  for (int i = 0; i < payloadLength; i++)
  {

    x = payload[i];
    if ( (x>='0' && x<='9')? (x-'0'):((x>='A' && x<= 'F')?((x-'A')+10):(0)) )
    
    {
      Serial.write(x);
    }

}

But it still sends ‘46’ for the received char ‘F’ and ‘42’ for character ‘B’. I need to send ‘15’ for the received char ‘F’ and ‘11’ for ‘B’.

nrkhara: But it still sends '46' for the received char 'F' and '42' for character 'B'. I need to send '15' for the received char 'F' and '11' for 'B'.

I suspect you have not described your requirement properly.

Can you post an example of a message that you receive and a corresponding example of what you want to send.

...R

nrkhara: But it still sends '46' for the received char 'F' and '42' for character 'B'. I need to send '15' for the received char 'F' and '11' for 'B'.

Please post the code that displays this behaviour.

Okay. I’m having this callback function where the variable payload of data type byte* gets the data from the the Bluemix server. Here is the code which i’m using.

void callback(char* topic, byte* payload, unsigned int payloadLength) 
{
  char x;
  for (int i = 0; i < payloadLength; i++)
  {
    x = payload[i];
    if ( (x>='0' && x<='9') ? (x-'0') : ( (x>='A' && x<= 'F') ? ( (x-'A')+10):(0) ) )
    {
      Serial.write(x);
    }

  }
}

So if payload contains ‘F’ the above code sends 46 which is the ascii representation.

What i want is that it should send 15 when payload receives ‘F’. Because F is 15 in hex.

I’ll give you another example of code.

int x = 0x0f;
Serial.write(x);

The above code sends 15 when i use Serial.write(x).

Now, have i made myself clear? If not please let me know. I’m really getting confused between the data type conversions over here.

Regards,

Nishit.

if ( (x>='0' && x<='9') ? (x-'0') : ( (x>='A' && x<= 'F') ? ( (x-'A')+10):(0) ) )
    {
      Serial.write(x);
    }
Serial.write((x>='0' && x<='9') ? (x-'0') : ( (x>='A' && x<= 'F') ? ( (x-'A')+10):(0) ) );

surely?

nrkhara: So if payload contains 'F' the above code sends 46 which is the ascii representation.

That's not enough information to provide a context.

Post an example of a complete message and explain what needs to happen in response to the whole thing. At the moment this is a typical XY problem

...R

Did you try the code in reply #9?