# How to convert HEX string to equivalent DEC integer

Hi guys,

I need a little help again. I’ve been searching the web all day yesterday but haven’t found the answer yet…

I have a two digit HEX string such as

``````"C8"
``````

and I need to convert this string to it’s equivalent decimal integer like so

``````200
``````

This way I can compare ranges like

``````i > 150 && i < 255
``````

which I can’t do at the moment since the value is a HEX string.

any help will be greatly appreciated!

which I can't do at the moment since the value is a HEX string.

First, some proof that "the value is a ... string" would be useful.

Second, is the string always two characters? If so, converting a value in the range '0' to '9' to a number is easy, as is converting a value in the range 'A' (or 'a') to 'F' (or 'f') is also easy. Then, multiply the first value by 16 and add the second value - where the values are the result of converting the 1st and 2nd characters.

Or, use strtoul().

PaulS:

which I can’t do at the moment since the value is a HEX string.

First, some proof that “the value is a … string” would be useful.

Second, is the string always two characters? If so, converting a value in the range ‘0’ to ‘9’ to a number is easy, as is converting a value in the range ‘A’ (or ‘a’) to ‘F’ (or ‘f’) is also easy. Then, multiply the first value by 16 and add the second value - where the values are the result of converting the 1st and 2nd characters.

Or, use strtoul().

Yes the String is always a two digit number. Here is how the String is generated.

``````while (Serial.available() > 0) {

String rssi = getToken(serialData, ',', 2);
...
...
...

String getToken(String data, char separator, int index) {
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;

for(int i = 0; i <= maxIndex && found <= index; i++) {
if(data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
``````

rssi is then the two digit string.

rssi is then the two digit string.

No, it is not.

PaulS:

rssi is then the two digit string.

No, it is not.

Ok, now I'm confused :D What is it then? :fearful:

What is it then?

A String. Which is not the same thing as a string. Not by a long ways.

If you had a string, you could deal with it the way I suggested. There is a function to extract a string from the String, so you could do what I suggested. Or, there is a function to extract one character at a time, so you could do what I suggested.

Or, you could loose the String crutch, and just deal with strings.

Since you're looking for the newline character, why not use:

``````Serial.readBytesUntil('\n', data, sizeof(data));
``````

I would also change data from String to char data[].

PaulS:

What is it then?

A String. Which is not the same thing as a string. Not by a long ways.

If you had a string, you could deal with it the way I suggested. There is a function to extract a string from the String, so you could do what I suggested. Or, there is a function to extract one character at a time, so you could do what I suggested.

Or, you could loose the String crutch, and just deal with strings.

Ok so I have a String, is there an easy way to go straight to int? If not, how do I go to string?

Ok so I have a String, is there an easy way to go straight to int?

Have you looked at the documentation for the String class? Have you tried anything? It would have taken less time to look and try than to ask.

Or, you could loose the String crutch, and just deal with strings.

Do you mean c-strings, aka a byte string stored in memory with a nul terminator appended?

C means 12.

So C8 means 12 x 16 + 8 = 200

It is the same concept as in decimal.

"214" means 2 x 100 + 1 x 10 + 4 = 214.