String with 0/1 representation of binary to convert

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Post Reply
LexRutter
Posts: 4
Joined: Sat Dec 05, 2020 9:52 am

String with 0/1 representation of binary to convert

Post by LexRutter »

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)
rlebeau
BCBJ Author
BCBJ Author
Posts: 1759
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: String with 0/1 representation of binary to convert

Post by rlebeau »

LexRutter wrote: Mon Jan 04, 2021 1:02 pm Edit1->GetTextBuf(Bin,Edit1->GetTextLen()+1);
i1 = (_int64)wcstol(Bin, NULL, 2);
will do the job, but it's a bit messy.
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);
However, do note that wcstol() returns a 'long', which is 32bits on Windows, so if you try to enter a number higher than 2147483647 then wcstol() will fail with an ERANGE error. You can use wcstoll() instead, which returns a 'long long', which is 64bits:

Code: Select all

__int64 i1 = wcstoll(Edit1->Text.c_str(), NULL, 2);
Alternatively, if you are using C++11 or later, you can use std::stoll() instead, which has overloads for both char* and wchar_t* strings:

Code: Select all

__int64 i1 = std::stoll(Edit1->Text.c_str(), nullptr, 2);
Remy Lebeau (TeamB)
Lebeau Software
LexRutter
Posts: 4
Joined: Sat Dec 05, 2020 9:52 am

Re: String with 0/1 representation of binary to convert

Post by LexRutter »

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.
rlebeau
BCBJ Author
BCBJ Author
Posts: 1759
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: String with 0/1 representation of binary to convert

Post by rlebeau »

LexRutter wrote: Wed Jan 06, 2021 2:53 pm Thus far, I have not found a String method that will do the same thing.
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().
LexRutter wrote: Wed Jan 06, 2021 2:53 pm Again, thanks, still trying to see if I can "modernize" this a bit.
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
Post Reply