Click here to Skip to main content
14,696,276 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I don't get why this is happening.

Im reading my 1tb hard drive size in WMI and its giving the correct result in bytes of [1000202039296]. Now if i use atoi on this char so i can do other math on it and convert it to MB and GB, but when i use atoi i get [2147483647].

Here's my code:
char TotalSpace[50];

ReadPropertyString( L"Size", TotalSpace );

printf( "[%s]", TotalSpace );
printf( "[%i]", atoi( TotalSpace ) );

This gives me the reults as above:


Can anyone explain why this is happening and a possible fix please?

atoi returns a 32-bit integer. Your disk size is bigger.
Try atof().

More here:[^]

Hope this helps,
Sure, can't quite work out the logic used to arrive at the number indicated (2147483647 - 0x7FFFFFFF - max value for signed int)

But I can tell you that atoi is designed to work on 32bit ints.

I think the function you're looking for is likely to be: _atoi64[^]

char TotalSpace[50] = "1000202039296";

printf( "[%s]\n", TotalSpace );
printf( "[%llu]\n", _atoi64( TotalSpace ) );

Perfect Pablo, it worked a treat

enhzflep 15-May-12 7:32am
Just be aware that you've now converted your integer value as represented by the string into a double. This produces warnings when asigning the return value of atof to an unsigned long long.

_atoi64 converts it to an unsigned long long.

But, atof is good enough for government work :p

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900