• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Char* to __int64

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

JigPu

Inactive Pokémon Moderator
Joined
Jun 20, 2001
Location
Vancouver, WA
Here's my latest problem for you all. I need a function that can convert a charachter array to a 64-bit integer in C++. I've already tried atoi() and atol(), but neither will convert the value correctly for me. I've tried creating my own function to do it, but that ended up being a nightmare that never worked.

If anybody can point me to a function (or write one) that will turn a charachter array like "1266516162473" to a __int64, I'd be grateful :)

JigPu
 
sprintf would go from an integer/long/etc to a string, that's the opposite of what he wants. He has a string, and he wants to make an int.

You could write a function to do it.

for example, you could do this (excuse the lack of indents, I can't get them to work on the boards for some reason):

__int64 stoa (char * numstring)
{

int i = 0;
__int64 mynum = 0;

while (numstring != '\0')
{
mynum *= 10;
mynum += numstring - 48;
i++;
}

return mynum;
}

This may not be super efficient, so if you have to do a lot let me know I'll come up with something better. It will work though. Note also that there is no check for overflows (numbers too large to be stored in your data type).
 
klingens said:
sprintf()

Which is unlike atoi() ANSI C

1) sprintf is the opposite of what he needs. sscanf would be better.

2) ANSI C does not specify conversions for __int64 in sprintf or sscanf. The Microsoft libraries that come with VS do have a %I64i format specifier. That hardly makes it ANSI C though.

3) atoi is ANSI C, but like sscanf it doesn't support __int64. Again Microsoft has provided an __int64 equivalent to this function called _atoi64.

4) __int64 isn't even part of ANSI C. It is an extension Microsoft has added to their compiliers, hence the double underscore. So there is no point in looking for an ANSI C solution.
 
Awesome, _atoi64 is exactly what I was looking for :)

Thanks a bunch guys!
JigPu
 
Back