Click to See Complete Forum and Search --> : conversions
synapsys
08-28-2003, 05:53 AM
Hi dudes,
Id like to ask a question.
I have a char* which is "2147483650" what i would like to do is convert this to an unsigned int so that when i do a printf("%u"); i dont get only the max value that a normal int can have, is there a function similar to atoi that i can use.
Thanks
cfaun5
08-28-2003, 09:30 AM
You can use atol (convert char* to long) and then cast to an unsigned int:
unsigned int i = (unsigned int)atol(string);
synapsys
08-28-2003, 05:11 PM
Actually i tried that and it didnt work but i used atoll and that was ok. Im pretty confused as to why atol didnt work.
Anyway thanks for your help
bwkaz
08-28-2003, 06:45 PM
What did atol do when it "didn't work"?
cfaun5
08-28-2003, 07:20 PM
It works fine on this end, also not to the numbero digits you specified: an unsigned int doesn't have the capacity to display that. However, chop off the last digit (the 0) and it works just fine. If you want greater precision, use a long long:
#include <stdlib.h>
#include <stdio.h>
int main() {
char c[] = "214748365";
unsigned int i = (unsigned int)atol(c);
printf("%d\n", i);
}
the simple solution: write your own or use the one i've written below for you.
unsigned int atoui(const char *s)
{
unsigned int total = 0;
while(*s != 0) {
total *= 10;
total += (*s) - '0';
s++;
}
return total;
}
synapsys
08-29-2003, 04:28 AM
stoe,
Thats some nice stuff man, i dont get it though could you explain, i dont understand why doing what you did gets the answer :confused:
Nice stuff though :)
an unsigned int doesn't have the capacity to display that. However, chop off the last digit (the 0) and it works just fine. If you want greater precision, use a long long:
Why doesnt an unsigned int be able to display that ? cause if thats the case then i guess that was my error, but why is it not able to display it.
That value can be represented by 32 bits and i assume an unsigned int is 32 bits long so why ? I think thats why atoll worked because it is greater precision a long long like you mentioned, i thought that a long being 64 would easily be able to represent that number and a long long would be far to many bits needed ?
What did atol do when it "didn't work"?
I have a function which checks each bit and prints out the binary value
What i need is 32 bits because thats what im sending though the socket and at the other end i need the value to stay as it is.
So when i sent a 32bit value which ended up being greater then what an int could represent when i printed the binary value it printed out
0111111111111111 ->(up to 32 bits) which is not what i wanted after the atol conversion.
For example i use an unsigned int and it might have the binary value
10010100001010001010000101001111 before i send it though, so what i did is change that to a char so it will have the value "2485690703" as a char for example. I send that value as a char* though the socket and then on the other end i need that converted from the char* to an unsigned int so that the binary value is still 10010100001010001010000101001111.
When i was passing atol("2485690703") it kept giving me 0111111111111111 ->(up to 32 bits) .. but when i used atoll it was ok, i'll investigate it further to see where i screwed up.
synapsys
08-29-2003, 05:19 AM
Stoe,
Soon after i hit submit i started to work on my code and then i worked out what it is ur doing.
Its cool and i think i'll use it.
Thanks for your help :)