I have a question in conversion of String into value.
Following is the sample code I tried.
I am trying this on C++ Builder XE4.
String strSize = L"64420392960"; // 64GB
size_t size;
size = strSize.ToDouble(); // returns 4290850816
char *end_ptr;
size = strtol(AnsiString(strSize).c_str(), &end_ptr, 10); // returns 0
Both of ToDouble() and strtol() didn't work.
I understand that strtol didn't work because long type is up to 4.3GB.
Are there any function in C++ Builder XE4, with which I can convert the strSize into size_t value when I treat 64GB or several hundreds of GB (eg 500GB)?
You don't have a 64GB string, which would be a string that's 64 gigabytes in size. You have a string containing a decimal number representing a value of about 64 gig.
The standard function you're looking for is strtoll
, which returns a long long
result. It's been a standard C function since the 1999 standard, and if I'm not mistaken was added to the C++ standard as of 2011.
(The question is whether C++ Builder XE4 supports strtoll
.)
If size_t
on your system is only 32 bits, there's no way to get a size_t
value that big. If it's 64 bits, strtoll
should do the trick.
The problem is that the VCL String
object (which is apparently an alias for the VCL
AnsiString
UnicodeString
class) returns a double
with the value 64420392960.0 which gets converted into an integral type but the size_t
is a 32-bit unsigned type so it keeps only the lower 32 bits of the value.
Instead of a type size_t
try an unsigned long long
or plain old long long
if your toolchain supports it.
Other people have explained why your original code does not work. There is another alternative: use __int64
instead of long long
. The VCL has support for __int64
, eg:
String strSize = L"64420392960";
__int64 size = StrToInt64(strSize);
Don't use double
unless you really need floating-point precision.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.