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?
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.