# ESP8266 ASCII to HEX convertion and checking

I am trying to convert ASCII to hex and check if the hex is equal to that hex that I want.

For example:

`1B 42 05` it means `B` in ASCII, I need to check if that hex is equal to `1B 42 05`.

Here I need to send converted hex code from ASCII code by using `SoftwareSerial`.

Example:

``````02 20 2D 33 30 32 35 30 20 6B 67 0D 0A 03
``````

I have to convert to

``````-30250 kg
``````
• you can't convert 1B to HEX because it IS HEXadecimal representation of a number. it is like if you would say that you need to convert 42 to a decimal number. you can convert string "1B" to a byte value. – Juraj Nov 27 '18 at 20:42
• `1B 42 05` is more than just a `B` ...... please consult ASCII tables – jsotola Nov 27 '18 at 21:28
• Your example looks like a serial protocol string... `02 20 2D 33 30 32 35 30 20 6B 67 0D 0A 03` corresponds to `<stx> -30250 kg<cr><lf><etx>`. So you have to do a bit more than just hex to ascii – Andre Courchesne Nov 28 '18 at 2:47

A little explanation about HEX and ASCII:

Raw computer data consists of zeros and ones. For humans it is difficult to read a (long) row of zeros and ones. You can represent such a row (of a certain length) by a decimal number, but then the relation with the underlying bits is not directly clear. For this reason the hexadecimal (HEX) representation is used (hexadeca comes from Greek and means 16.
Four bits can represent 16 different situations (= 16 different combinations of zeros and ones). The HEX digit belonging to each combination is:
0000 = 0; 0001 = 1; 0010 = 2; 0011 = 3; 0100 = 4; 0101 = 5; 0110 = 6; 0111 = 7
1000 = 8; 1001 = 9; 1010 = A; 1011 = B; 1100 = C; 1101 = D; 1110 = E; 1111 = F
When you memorize these 16 cases (from which the first 10 are easy because it is juist binary counting), you know immediately which bit-pattern belongs to a hexadecimal number and vice versa. For example: the HEX value 5A = 01011010.

Computer data contain not only numbers, but also text. As the computer can only handle bits (zeros and/or ones) we have to decide how letters (characters) are presented by bit patterns. And preferably in a standardized way, in order that different computers can handle each others text. Unfortunately there is more than one standard, but one that is very much used is the ASCII standard (see: https://en.wikipedia.org/wiki/ASCII).
The ASCII standard uses 7 bits codes and defines not only characters but also 'control codes' e.g. for 'line feed' and 'carriage return'. Often these 7 bits are stored in one byte (with the leftmost 8th bit set to 0) and can than be represented by two HEX digits.

Without further information nobody can tell what a row of bits represents. That's one of the reasons datatypes are used. They tell us what the row of bits represents and how many bits (or bytes) are used for the representation.

So the HEX representation of your row of 3 bytes is "1B 42 05" and, when these are characters, could stand for (look at the ASCII table): "ESCAPE B ENQ". In which 'ESCAPE' and 'ENQ' are control codes which meaning depends on the communication protocol. The second row (of 14 bytes) could stand for: "STX SPACE - 3 0 2 5 0 SPACE k g CR LF ETX". STX and ETX stand for 'START OF TEXT' and 'END OF TEXT'. 'LF' stands for 'LINE FEED' and 'CR'stands for 'CARRIAGE RETURN'.

I hope that with this explanation you have a better idea about HEX and ASCII and that you now will be able to figure out how to handle your data.

Converting an ASCII HEX digit is simple enough. Here's a little function I use all the time:

``````uint8_t h2d(char hex) {
if(hex > 0x39) hex -= 7; // adjust for hex letters upper or lower case
return(hex & 0xf);
}
``````

Pass it a single HEX digit as a `char` variable and it returns a number between 0 and 15.

To convert a pair of characters into an 8-bit value just call it twice with each character, shift one of them 4 bits to the left, and OR them together. For instance:

``````uint8_t h2d2(char h1, char h2) {
return (h2d(h1)<<4) | h2d(h2);
}
``````

That would give you a number between 0 and 255 (or 016 and FF16, or 08 and 3778...)

How you receive and split up the incoming data properly is a whole other topic, but this and this could probably help you.