1

problem is to convert a string of binary digits into its decimal representation. Easy eh?

well, it needs to be able to handle input of over 64 bits in length and convert it without using any external libraries or data types like big integer.

How can I do this?

so far i have a string called input which handles the binary

I then access each digit using input[0] etc to get a char representing that digit.

Now I manipulate it and multiply by the corresponding power of 2 that its index represents, and move through the array storing the total as i go.

I use a big integer to store the total as for large numbers the primative types dont work.

My first solution works perfectly, how can I do this without using anything to store the total, i.e only using strings to store answers.

Any Ideas?

Thanks

1
  • If it's homework, please state/tag so clearly. Commented Mar 28, 2011 at 19:33

1 Answer 1

2

You will need an array of digits to hold the result. An array of int's would be easier but you can also use the final string. You can calculate it's length from the length of the inputstring, you may have to remove leading zero's in the end.

Calculate your result as before but do the adding (including the carrying) in the result array.

I think this is a (homework) assignment that wants you to implement a version of the "pen & paper" addition method.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.