• Forum
  • Lounge
  • Mathematicians can you explain how to co

 
Mathematicians can you explain how to convert binary to a string.

closed account (G30GNwbp)
I would like to be able to convert 1111 1111 to the string "255" with an algorithm that would work for any number of bytes. Does anyone know how it is done?

Please do not provide code
Last edited on
so your asking how to take a number and convert it into characters? ( remember, binary is just a counting system )

I will tell you my general concept for how i implemented something like this
( its not the most efficient way, but it might get your brain juices flowing and youll come up with a better one )

my really simple way of converting an integer to a string was to first find how many digits where in the number ( when in decimal form ). after that i subtracted 1 from the highest digit place and counted how many times i had to do that before that digit was gone.


closed account (G30GNwbp)
I looked at this website but I am not sure I understand.
http://www.electronics-tutorials.ws/binary/bin_2.html

Is this the only way to do it:

1111 1111 % 1010 = 101 or 5
1111 1111 / 1010 = 11001
11001 % 1010 = 101 or 5
11001 / 1010 = 10 or 2



This should probably go in the beginners section.

If it's just converting a number to a string you can use std::to_string() or sprintf().
I gave this a try and my solution was
- in a loop, starting from the first character
-- convert one character to digit
-- calculate the value of that digit in its specific place (that is, 1 in 100 is 4)
-- add to a total
-- repeat with next char
- convert total to string with formerly mentioned functions

This SHOULD THEORETICALLY work with any number up to 31 bits, sign character excluded.

I purposefully left out the actual formula as I'm guessing this is homework, but I hope this can help you.
Last edited on
closed account (G30GNwbp)
@maeriden
My goal is to use the standard library as little as possible, and work with very large numbers so your solution would not work.
Last edited on
And I definitely want to work with number which need more than 63 bits.
Well, the algorithm itself works for any number of bits, but since I used a long to store the decimal value before converting it to a string, it is guaranteed to work only up to 31 bits, since long is guaranteed to be at least 32 bits wide (a 32-bit string can represent a value that a signed long may not be able to hold. Using unsigned long would allow you to use one extra bit but you can't convert negative numbers).
That being said, I guess on most system a long is 64 bit now.

I am curious do think converting a binary number that was say 300 bits to decimal is beginners work?
Probably not, as you most likely need a bigint library to handle data of that width, or a different algorithm than the one I used. And definetly not if you don't want to use any library,

I'm not aware if or how a processor can calculate numbers that are wider than 64 bits, and I can't come up with a good alternative so I can't help you more than this.
Last edited on
closed account (G30GNwbp)
I'm not aware if or how a processor can calculate numbers that are wider than 64 bits, and I can't come up with a good alternative so I can't help you more than this.


I think I can calculate large number (that is my goal). I know it has been done many times, I just want to try it myself before I look at other solutions.
Last edited on
closed account (S6k9GNh0)
Using current solutions, we would just convert the [string of binary to integer], then convert it back to string in decimal.
closed account (G30GNwbp)
My ideal might be silly/foolish, but I want to simulate a very large register that I can use to calculate large numbers. Then I want to take the calculated amount and convert it to a decimal string.

It doesn't seem to me that it would be that hard. And I thought it would give me a better understanding of the bitwise operators which I am very weak at.

Every project I start always seems to be easy in my mind's eye.
For any arbitrary number of bits you are talking bignums.

Since binary and decimal representations don't jive very friendly-like, you need to have an algorithm to perform division.

To convert any number (source representation doesn't matter) to decimal:

1
2
3
4
5
6
string result = ""
while (number != 0)
{
  result = (char)((number % 10) + '0') + result;
  number = number / 10;
}

The gradeschool "shift and subtract" method is particularly convenient when working with binary digits, and it produces both a quotient and a remainder at the same time. (Something your algorithm may make use of.)
http://courses.cs.vt.edu/~cs1104/BuildingBlocks/divide.030.html

1
2
3
4
5
6
7
8
string result = ""
while (number != 0)
{
  int remainder;
  bignumber quotient;
  divide_by_integer( number, &quotient, &remainder );
  result = (char)remainder + result;
}

Hope this helps.
My ideal might be silly/foolish, but I want to simulate a very large register that I can use to calculate large numbers.
This seems interesting. I'll make an attempt at it. It's been a lot since I coded something stimulating.

I'm using http://www.waitingforfriday.com/index.php/4-Bit_Computer for bitwise addition reference.
I don't think OP is interested in that low-level a problem...
Well, if you're converting... http://www.eng.utah.edu/~nmcdonal/Tutorials/BCDTutorial/BCDConversion.html is exactly what you want. The end result leaves it in binary representations of single digits in their respective spot, without requiring modular division. Only addition. As a bonus, compared to all prior methods discussed, this can actually be done mechanically with ease. All it requires is a clock. Also, you can't actually convert binary to decimal. You can only mathematically convert it to BCD, or binary-converted-decimal. The next step would be the modular-division part.
Last edited on
Topic archived. No new replies allowed.