14,696,276 members
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];

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

This gives me the reults as above:

[1000202039296]
[2147483647]

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

Solution 1

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

More here:
http://msdn.microsoft.com/en-us/library/hc25t012(v=vs.71).aspx[^]

Hope this helps,
Pablo.
v2

Solution 2

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[^]

EDIT:
```char TotalSpace[50] = "1000202039296";

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

Output:
[1000202039296]
[1000202039296]
v2

Solution 3

Perfect Pablo, it worked a treat

thankyou
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