strtol Stuck with big numbers

I’m trying to convert Hex to Dec,

Dec = strtol(Hex, NULL, 36); //works fine(not sure if I’m misusing some values there but it works... until

I have Hex == 90c2ea61 or similar.

Returns Dec = 2147483648.
That’s hex 7fff ffff or max long int.

I have tried unsigned long, float also.
Google searches suggest unsigned long should be fine but I’m stuck.
Help appreciated!

Mattty5:
I’m trying to convert Hex to Dec,

I assume by "Hex" you mean "hexadecimal". That being true, hexadecimal is a base 16 number system.

Does the text represent a signed or an unsigned number? Which type is the variable Dec?

That's why we ask to post complete code (or a representative example that exhibits the behaviour) so we don't have to guess. If unsigned, fix the above pointed out mistake and make Dec an unsigned variable and use strtoul().

char hexText[] = "90c2ea61";

void setup()
{
  Serial.begin(57600);

  uint32_t dec32u = strtoul(hexText, NULL, 16);
  Serial.println(dec32u, HEX);
  Serial.println(dec32u, DEC);
}

void loop()
{
}

What is the maximum value that is represented by the text? Will it fit in an uint32_t or do you need a bigger variable.

Which architecture are you using? 8-bit AVR or 32-bit processor? There is limited support for 64-bit numbers in the AVR implementation in the Arduino environment. It seems that e.g. strtoll() (note the double 'L') is not available; in which case you need to roll your own. And there is no print function to print 64-bit numbers as a any text representation.

1 Like

Thank you, good guessing, how do I add the pretty box for my code?

Thank you very much, I removed strtol and replaced it with strtoul and it works perfectly!

I’d never of figured that out, I only knew of strtol from about 4am... I’m finding escaping the Arduino examples pages into ‘real code’ very tough.

I think you missed a big hint in reply #1.

Let's try againDec = strtol(Hex, NULL, 36); ?

Oh yeah, and I changed 36 back to 16,
As in [16] {a,b,c,d,e,f,0,1,2,3,4,5,6,7,8,9}
?? :slight_smile:

No, 16 as in {0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f} :wink:

somebody confiscate my keyboard
Thank you

Mattty5:
how do I add the pretty box for my code?

type
** **[code]** **

type / paste your code after that
type
** **[/code]** **
after that

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.