### STRTOUL not working as expected

Mon, 08/22/2011 - 16:39

### Forums:

I have the following two lines:

char* strSeq = "02b9aa49605a5b8d"; unsigned long long seq = STRTOUL(strSeq, NULL, 16);

After exection seq is 0xFFFFFFFF.

How can I get a numerical representation of the string "02b9aa49605a5b8d"?

Tue, 08/23/2011 - 09:05

0x02b9aa49605a5b8d is 60 bits long in its binary representation, so you are telling me that can't fit in a unit64 which advertises itself as 64 bits long?

0x02b9aa49605a5b8d in decimal is 196375290893196173, while the maximum range of an unsigned 64 bit int is 18,446,744,073,709,551,615.

Is 196375290893196173 less than 18,446,744,073,709,551,615? Why yes it is, by quite a lot actually.

What about this:

uint64 seq = (uint64)STRTOUL("100000000", NULL, 16);

0x1000000000 is only 68719476736 in decimal, which is 1 followed by 36 0s in binary.

Yet when that code is run on the simulator the result is also 0xFFFFFFFF if you run it on the simulator.

0x02b9aa49605a5b8d is 60 bits long in its binary representation, so you are telling me that can't fit in a unit64 which advertises itself as 64 bits long?

0x02b9aa49605a5b8d in decimal is 196375290893196173, while the maximum range of an unsigned 64 bit int is 18,446,744,073,709,551,615.

Is 196375290893196173 less than 18,446,744,073,709,551,615? Why yes it is, by quite a lot actually.

What about this:

uint64 seq = (uint64)STRTOUL("100000000", NULL, 16);

0x1000000000 is only 68719476736 in decimal, which is 1 followed by 36 0s in binary.

Yet when that code is run on the simulator the result is also 0xFFFFFFFF if you run it on the simulator.

Tue, 08/23/2011 - 10:30

Let me clear, Values 0x02b9aa49605a5b8d and 0x100000000 are not in the range of 32 bits. If you look into the documentation helper function STRTOUL returns max 32 bit number.

here it is from API reference:

uint32 STRTOUL( const char *nptr, char **endptr, int base );

In your case 0x02b9aa49605a5b8d and 0x100000000 both are out of 32 bit range and thats why you are getting Max 32 bit number in hex would be 0xFFFFFFFF which is defined in aeestddef.h as #define MAX_UINT32 4294967295u(0xFFFFFFFF)

Let me clear, Values 0x02b9aa49605a5b8d and 0x100000000 are not in the range of 32 bits. If you look into the documentation helper function STRTOUL returns max 32 bit number.

here it is from API reference:

uint32 STRTOUL( const char *nptr, char **endptr, int base );

In your case 0x02b9aa49605a5b8d and 0x100000000 both are out of 32 bit range and thats why you are getting Max 32 bit number in hex would be 0xFFFFFFFF which is defined in aeestddef.h as #define MAX_UINT32 4294967295u(0xFFFFFFFF)

Tue, 08/23/2011 - 11:11

Thanks for the clarification.

So is there any alternative?

Thanks for the clarification.

So is there any alternative?

Wed, 12/28/2011 - 00:10

You may want to write STRTOUL wrapper

You may want to write STRTOUL wrapper

Tue, 08/23/2011 - 10:04

It is working, In your case you are using "0x02b9aa49605a5b8d" large value and is causing overflow.

char* strSeq = "0x02b9a";

uint32 seq = (uint32)STRTOUL(strSeq, NULL, 16);

Above code worked for me.

It is working, In your case you are using "0x02b9aa49605a5b8d" large value and is causing overflow.

char* strSeq = "0x02b9a";

uint32 seq = (uint32)STRTOUL(strSeq, NULL, 16);

Above code worked for me.