2

If the string is "8123", I first converted the string number into its integer form 8123 and then sent this number to a function that converts it to binary. I got numbers as big as unsigned long long to work but once its passed that, the outputs are wrong. Is there a way to convert to binary by looking at each digit. i.e looking a the 3, 2, 1, and 8 to convert to binary.

So rather than taking the string "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" and turning it into a number, is there a way to look at each character in this string and turn it into binary?

Any suggestions is much appreciated

1
  • It's not clear what you're asking. Perhaps you can show some code, or at least show code that you'd like to be able to use that would better illustrate what you mean. Commented Apr 12, 2014 at 0:31

2 Answers 2

1

Pseudocode:

string binary_string = "" #an example number = 81 while (number != 0) #append the string casted value of the remainder of (number / 2) #to the front of binary_string binary_string = str(number % 2) + binary_string number = number / 2 

e.g. 81:

binary_string = str(81 % 2) + binary_string = str(1) + "" = "1"

number = 81 / 2 = 40

binary_string = str(40 % 2) + binary_string = str(0) + "1" = "01"

number = 40 / 2 = 20

binary_string = str(20 % 2) + binary_string = str(0) + "01" = "001"

number = 20 / 2 = 10

binary_string = str(10 % 2) + binary_string = str(0) + "001" = "0001"

number = 10 / 2 = 5

binary_string = str(5 % 2) + binary_string = str(1) + "0001" = "10001"

number = 5 / 2 = 2

binary_string = str(2 % 2) + binary_string = str(0) + "10001" = "010001"

number = 2 / 2 = 1

binary_string = str(1 % 2) + binary_string = str(1) + "010001" = "1010001"

81 -> "1010001"

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

3 Comments

Thats similar to what I did. However, if the number is really, really big, it will print out the wrong binary. For example, I put 123123123123123123123123123 into my program and it prints out the wrong values. Small integers are printed out correctly
"123123123123123123123123123" is way too big to fit into a 32 bit integer and also too big to fit into a 64bit integer.
i guess thats why is there a way to look at each character in the string and somehow convert it into binary. i think that should allow the handling of big numbers?
1
string dec2bin(string in) { for(size_t i = 0; i < in.length(); i++) in[i] -= '0'; string out; while(in.length()) { out.insert(0, 1, '0' + (in[in.length()-1]&1)); char overflow = 0; if(in[0]<=1) { overflow = 10; in.erase(0); } for(size_t i = 0; i<in.length(); i++) { in[i] += overflow; overflow = 10 * (in[i]&1); in[i] /= 2; } } return out; } 

5 Comments

thanks deduplicator. unfortunately when I tried to compile it, it gave me errors for indirection requires pointer operand ('int' invalid) *a = *a-'0'; I am also unfamiliar with c++11, is it possible for you to translate to regular c++? thanks
removed all advanced C++ things.
The number must not be 0-padded, though a lone 0 is properly transformed. You have been warned.
what does 0 padded mean? it doesnt contain 0?
It means it is not "0", but it starts with a "0"-prefix. Left that for you to fix. It should be easy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.