# Convert hex to decimal

I want to convert a hex number 80000000 to decimal, however, the number is very long and I cannot find a code that works yet. Can someone point me in the right direction. I am currently using a string and HAVE to use a string, but I can modify it after.

Update: Sorry for the late replies, I am currently using this code with chat gpt. I am using a sensor that has a base value of 0 which is 80000000 = 0. The sensor can read positive and negative values, with positive being 80000001 = 1 and negative being 7FFFFFFF = -1 etc. So, I basically want to take the hex value which I have as a string, example 80000000 and convert it into a decimal, in order to get a numerical value for the sensor. I am using the function temporarily to just get readings with the serial monitor, but if there is a better way please let me know. Any help is greatly appreciated.

``````long hexStringToDecimal(String hexString) {
long decimalValue = 0;
int hexLength = hexString.length();

for (int i = 0; i < hexLength; i++) {
char c = hexString.charAt(i);
int digitValue;

if (c >= '0' && c <= '9') {
digitValue = c - '0';
} else if (c >= 'A' && c <= 'F') {
digitValue = 10 + (c - 'A');
} else if (c >= 'a' && c <= 'f') {
digitValue = 10 + (c - 'a');
} else {
// Handle invalid characters if necessary
continue;
}

decimalValue = (decimalValue * 16) + digitValue;
}

return decimalValue;
}
``````

Below is a picture of the serial monitor. Boxed in red is the hex outputs from the sensor. From the hex outputs, I am attempting to get the decimal value so that I can get a milliamp reading.

To all the replies I apologize, I am not too familiar with Arduino and programming in general so I am learning as I go but thank you for all the help!

When you say "convert", I assume you mean "obtain the hex representation as a text string"?

The whole point of Hex is that each digit represents exactly 4 bits of the number.
So all you need to do is mask-off 4 bits (a "nibble") at a time - then each nibble gives you one hex digit from the number.

Alternatively, just use (s)printf with `%x` ...

1 Like

If the number is stored in the computer, it is binary. Please provide useful details.

If it is a string of characters, use one of the many functions like atoul(), atoll() etc.

2 Likes

Or is it that you have a string(series of characters) "80000000" which you wish to convert to a numeric value?

1 Like

How long? The number that you gave fits in a 32bit (unsigned) integer

Below code demonstrates the use of strtoul() to convert text to an uint32_t. If the text that you want to convert is signed, use strtol(). If the number is 64bit, you can use strtoull() (note that you can't print 64bit numbers (easily) in Arduino. Obviously you need to adjust the variable type to cater for those differences.

``````char text[] = "80000000";
uint32_t uint32 = 0x80000000;

void setup()
{
Serial.begin(115200);
Serial.println(uint32);
Serial.println(uint32, HEX);

char *endptr;
uint32_t val;

val = strtoul(text, &endptr, 16);

if (endptr == text)
{
Serial.print("'");
Serial.print(text);
Serial.println("' does not contain hex digits");
}
else
{
Serial.print("'");
Serial.print(text);
Serial.print("' converted = ");
Serial.println(val);

if (*endptr != '\0')
{
Serial.print("''");
Serial.print(text);
Serial.println("' contains more charaters");
}
}
}

void loop()
{
}
``````

You have to harden this a bit (e.g. check if it's more than 8 hex digits long); e.g. '80000000abc' gives (for me) an unexpected result.

2 Likes

Is your number a natural binary or of 2's complement form, and accordingly it will be:

2,147,483,648

or

- 2,147,483,648

Which one you want to see on the display?

1 Like

Lots of questionsâ€¦ where is the OP?

1 Like

Use `strlen()` and subtract 1 (to ignore the null). That will give you 7. Take the first character `text[0] = 8`, use `atoi()` on it, then multiply it by 16^7. Accumulate the result. Repeat for the remaining characters.

Wait, you're a chatbot. Disregard.

1 Like

I know that you did answer the OP. Just FYI:

I specifically did not use `atoi()` as it will happily convert the character 'z' to 0

1 Like

It is as easy as a single line of the code:

``````uint32_t hex_number = 0x80000000;
Serial.print(hex_number, DEC);
``````

Hint: "DEC" in the print can be omitted, because print() outputs decimals by default.

1 Like

Right! But in this case we better specify to the OP that the strtoul() last parameter instructs the function to interpret the string as hexadecimal (aka the base is 16). Without it, the string "80000000" will be converted to 80,000,000 instead of 2,147,483,648.

We're global, maybe he's sleeping now...

Anyway, I'm not convinced he really needs such conversion, see the other posts he made before...

1 Like

The top code of the above has stored 1000 0000 0000 0000 0000 0000 0000 0000
in memory. What is the reason that you have chosen unint32_t data type instaed of int32_t data type?

The OP has given a number in hex base; he has not told if the corresponding binray number is in natural binary (positive) form or in 2's complemet form (could be positive or negative).

It is the user/programmer who knows about the nature of his number; the Computer knows nothing. It simply performs binary operation on the given data. For example:

``````10001000 =    88 in BCD format
10001000 =   138 in natural format
10001000 = -   8 in SM format
10001000 = - 120 in 2's complement form
``````
1 Like

I just learned that atoi() wants a null terminator, and does not handle single characters.

1 Like

It deos handle single charcater.

``````char myData[4];

void setup()
{
Serial.begin(9600);//UNO's UART is active
}

void loop()
{
byte n = Serial.available();
if ( n != 0)
{
byte m = Serial.readBytesUntil('\n', myData, sizeof myData - 1);
myData[m] = '\0';
int x = atoi(myData);
Serial.println(x, DEC);  //shows: 7
}
}
``````
1 Like

I guess @xfpd means a single `char`, rather than a 1-character string - would would also have the NUL termination?

EDIT

So not this:

``````char myData = '7';

int x = atoi( &myData );
``````
2 Likes

You're right. But have a look at his other topics (like THIS), to see the data comes from a Honeywell sensor. And I'm still convinced what we see here is the top of the iceberg of his implementations (or problems...).

BTW, the OP has the ability to make cross-posting about the same problem, so moderators should detect that sooner or later...

3 Likes

The atoi() function expects a null terminated array; where, myData of post #15 is not a null terminated array.

`char myData[] = "7";`
will be accepted by atoi() function.

1 Like
``````80000000h = 0 mA
7FFFFFFFh = -1 mA
80000001h = 1 mA
``````

What is the formatting base of the above numbers -- any clue?

Yeah, I agree it isn't unsigned.
BTW I never said it wasn't I'm just pointing out that it seems to me the OP isn't really getting a hex string, I can't imagine a sensor sending data as a hex string instead of an int32.

1 Like

I understand that a sensor sends data in a customized protocol or ususaly in UART/I2C/SPI and very rare in raw binary.

1 Like