Moorag
Moorag

Reputation: 93

converting a string of binary into a string of decimal c#

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

Upvotes: 1

Views: 682

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273244

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.

Upvotes: 2

Related Questions