I'm working on a project and need to convert a Unicode string (in Edit1->Text item) that has input limited to "0" and "1", which represents a binary integer. This needs to be taken and converted to an integer (preferably Int_64, but that's not set in stone). Using:
Edit1->GetTextBuf(Bin,Edit1->GetTextLen()+1);
i1 = (_int64)wcstol(Bin, NULL, 2);
will do the job, but it's a bit messy.
I have been unable to find a routine that works similar to the wcstol(BUFFER, End ID, Base) routine for a wchar_t variable for the String variable.
I'd appreciate any pointer to a conversion for Unicode Strings that lets me specify the base. Likewise, I'd also appreciate any suggestions on how to actually phrase a search that might help find similar items. I've exhausted my vocabulary of composing searches of the various C++Builder help files.
(I am using C++Builder 10.4.x Pro-version)
String with 0/1 representation of binary to convert
Moderator: 2ffat
Re: String with 0/1 representation of binary to convert
That can be simplified by getting rid of the character buffer completely. You can use the UnicodeString as-is, eg:
Code: Select all
__int64 i1 = wcstol(Edit1->Text.c_str(), NULL, 2);
Code: Select all
__int64 i1 = wcstoll(Edit1->Text.c_str(), NULL, 2);
Code: Select all
__int64 i1 = std::stoll(Edit1->Text.c_str(), nullptr, 2);
Remy Lebeau (TeamB)
Lebeau Software
Lebeau Software
Re: String with 0/1 representation of binary to convert
Thanks for the input, and I understand your points.
The real issue here is that the user is entering a binary number in text form: "1111 0101 0011". That does not normally convert to an actual integer the represents the binary number. So far, the only thing I've found is that one conversion call. It allows me to use binary, octal, hex, decimal. Thus far, I have not found a String method that will do the same thing.
FYI - I'm using the Int64 variable for some consistency in other parts of the process. In this case, it's really a simple integer.
Again, thanks, still trying to see if I can "modernize" this a bit.
The real issue here is that the user is entering a binary number in text form: "1111 0101 0011". That does not normally convert to an actual integer the represents the binary number. So far, the only thing I've found is that one conversion call. It allows me to use binary, octal, hex, decimal. Thus far, I have not found a String method that will do the same thing.
FYI - I'm using the Int64 variable for some consistency in other parts of the process. In this case, it's really a simple integer.
Again, thanks, still trying to see if I can "modernize" this a bit.
Re: String with 0/1 representation of binary to convert
Because there isn't one, not in the Delphi-based RTL anyway. The RTL has no concept of binary-encoded or octal-encoded strings. It does, however, have functions for decoding hex-encoded strings, via System::Sysutils::StrToInt() (and StrToInt64()), and System::Classes::HexToBin().
The standard C++ std::bitset class has a constructor that can decode a binary-encoded string into a sequence of bits, which you can then assign to an 'unsigned long int' or 'unsigned long long int' variable.
Remy Lebeau (TeamB)
Lebeau Software
Lebeau Software