3

Possible Duplicate:
Converting string of 1s and 0s into binary value

I would like to convert a binary string to an integer string in c++. e.g:

"000000000010" becomes "2"

I have the following constraint: i can not convert this string to an integer type and then convert it again to string because that binary string can be very huge!

I already tried to do it with stringstream, inserting the binary string to it with the flag std::stringstream::binary and then output the content using the std::dec flag, but it doesn't work

Thank you all in advance.

11
  • 4
    Welcome to Stack Overflow! We encourage you to research your questions. If you've tried something already, please add it to the question- if not, research and attempt your question first, and then come back. Commented Aug 21, 2012 at 14:06
  • 2
    This sounds like homework, if it is so, please tag it as such. Commented Aug 21, 2012 at 14:07
  • In order to fit into an int the string can't be very large, since an int typically only holds 32 bits, making the effective max length of the string 32 characters long. Commented Aug 21, 2012 at 14:07
  • 1
    This project requires minimum research that you should be able to find yourself without issue. Commented Aug 21, 2012 at 14:07
  • 1
    to anybody who wants to close the question: it is not an exact a duplicate of aforementioned question. The OP clearly states that he need a conversion to string, without any integer intermediate state, so any answer to that question will not work here. Commented Aug 22, 2012 at 11:10

3 Answers 3

11

You can construct a std::bitset from a std::string, and use std::bitset::to_ulong or std::bitset::to_ullong to get an integer. Be sure to take care of endianness, it is not done for you.

If you need a string from the integer, use std::to_string.

Sign up to request clarification or add additional context in comments.

6 Comments

Umm, the problem statement says that the binary string can be huge, so, presumably, an unsigned long long won't have the capacity to represent the same value. I don't see how endianness comes into play here.
Just as Pete states, as I read the question, an integer string is required as result type, not an integer type...
i can not do that because the number can exceeds an unsigned long long. :/ by the way this is not homework
if you convert it to an integer at some point, it still doesn't respect the fact that the binary might simply be too big for an integer
@nyarlathotep true. I missed that. Manual string arithmetic is the only thing possible now.
|
4

You could go about it like this:

  • create a new output string, containing "0"
  • for each character of the (reversed) original string:
    • multiply the result by 2 (digit by digit, taking care of the carry)
    • if the digit is a '1', add 1 to the result (as above)
  • reverse the result (use std::reverse)

7 Comments

as i said i can not do that.. overflow on too large representation
@jav974: I don't see the problem -- the largest number I am dealing with here is a single digit (maybe times 2), so it won't exceed 18
let's say i have a string representing (from right to left) 150 times 1 then 100 times 0 then again 200 times 1. that string's length is 450. I don't know any integer type that can handle 450 bits. May be i didn't understand the way you invite me to proceed :/
Why reverse? Try your algorithm on "1000". Without reversing should work, reversing will give you "1".
@TadeuszKopec: the natural way to operate on strings (especially if you need to append one, which might be the case here) is left-to-right; arithmetic with carry is done lower-to-higher-magnitude, which is right-to-left -- thus the reversal at the end
|
0

Off the top of my head, I'd start with an accumulated value of 0 and read the binary string from right to left. For each bit, multiply the accumulated value by 2 and if the bit is 1, add 1 to the accumulated value. If the accumulated value is greater than 9, divide it by 10 and write out the remainder as a character. That will give the decimal characters in reverse order.

Never mind. This doesn't work.

9 Comments

i can not accumulate the values beacause it would overflow with a string representing more than 64 bits ( and it will ! )
Off the top of my head, I'd put the conversions in a function, and pass the base as an argument:-).
@jav974 - no, the accumulated value won't overflow an integer. The divisions by 10 take care of that; it only works with one digit at a time, and the largest value it will see is 19.
@jav974 So use an integral type with more than 64 bits. (There are BigInteger classes available on the net.) Anyway you do it, there will be some sort of range restriction; just use an appropriately sized integral type.
@PeteBecker: I guess there should be a "merge answers" option, so we don't have to duplicate comments ;)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.