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