unsigned short a;
char temp[] = "70000";
a = atoi(temp);
printf("a: %d\n", a);
Gives me the output a: 4464
when it should be a: 70000
Is there a better way to convert from ASCII to a decimal? The range of a unsigned short is 0 - 65535
As schnaader said, you may be running into an overflow problem.
But answering your printf
question about outputting unsigned values, you want the u
modifier (for "unsigned"). In this case, as Jens points out below, you want %hu
:
printf("a: %hu\n", a);
...although just %u
(unsigned int
, rather than unsigned short
) would probably work as well, because the short
will get promoted to int
when it gets pushed on the stack for printf
.
But again, that's only if the value 70000 will fit in an unsigned short
on your platform.
You are answering the question yourself. The range of a unsigned short
is 0-65535, so 70000 doesn't fit into it (2 bytes), use a datatype with 4 bytes instead (unsigned int
should work, you can check the size with sizeof
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With