## 7 replies

Last post Sep 18, 2015 09:06 PM by gerrylowry

• sham25

None

0 Points

3 Posts

### Converting HUGE Binary numbers to Decimal in C# HOW?

I want to convert 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 to its decimal equivalent and no I am not wanting to display 5092 + e to the something , I am trying to figure out how I can actually display to the user this: 509258994083621521567111422102344540262867100557355601717974933926024279115076881119969280 . Before anyone says it, no I can not use the

`BigInteger`

datatype to help. Any ideas of how I can do this? I heard that you can actually like store 0-9 in binary or something and do that when you take input as a vector of strings or something? I was kind of confused about the process. I guess i would store as strings some  where 001, 010, 100.. somewhere in code and somehow reference that? Idk any help would be GREATLY appreciated. I'm very lost on what to do.

• gerrylowry

Star

14297 Points

5797 Posts

### Re: Converting HUGE Binary numbers to Decimal in C# HOW?

@sham25 welcome to forums.asp.net

Let's look a your 90 digit integer:

```509258994083621521567111422102344540262867100557355601717974933926024279115076881119969280
123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x```

Your binary number is 299 binary characters:

```10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789```

Your binary number expressed as a decimal integer would be the sum of:

``` 298          150          149
2       +    2       +    2   ```

c# lets you use the long value type:  https://msdn.microsoft.com/en-us/library/ctetwysk.aspx "long (C# Reference)"

```long
–9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 is 19 digits.
Signed 64-bit integer
System.Int64 ... .NET Framework type```

However, even 2^149 is a huge integer =~ 7.1362385e+44,
i.e.  713623850000000000000000000000000000000000000

sham25, the .NET Framework supports standard arithmetic; even System.Decimal, a 128-bit datatype, supports < 30 significant digits:

```decimal
(-7.9 x 1028 to 7.9 x 1028) / (100 to 28)
28-29 significant digits
System.Decimal ... .NET Framework type```

sham25, the general answer to your question is that your large numbers need to be represented as custom objects supported by custom code ... written by you or a third party.

THE BIG QUESTION is WHY would you want to do this?  Please explain.

References:

https://msdn.microsoft.com/en-us/library/364x0z75.aspx "decimal (C# Reference)"

https://msdn.microsoft.com/en-us/library/s1ax56ch.aspx "Value Types (C# Reference)"

EDIT:

such calculations are possible, but not with the standard .NET Framework.

one example:   https://defuse.ca/big-number-calculator.htm

2^149 =
713623846352979940529142984724747568191373312

2^298 =
509258994083621521567111422102344540262867098416484062659035112338595324940834176545849344

END EDIT.

EDIT # 2:

Taylor Hornby's calculator can also handle expressions like:

2^298 + 2^150 + 2^149 =
509258994083621521567111422102344540262867100557355601717974933926024279115076881119969280

END EDIT # 2.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• sham25

None

0 Points

3 Posts

### Re: Converting HUGE Binary numbers to Decimal in C# HOW?

It's a homework problem for school. I think I need to use the Double Dabble Algorithm. Unfortunately, I can't find any examples of source code for the Double Dabble Algorithm anywhere - and I'm trying as hard as possible to not re-invent the wheel here lol.

• gerrylowry

Star

14297 Points

5797 Posts

### Re: Converting HUGE Binary numbers to Decimal in C# HOW?

it would have been best for you to have been honest with your peers here at forums.asp.net from the beginning about this being a homework problem.

FWIW, i am a former community college computer science instructor who has been writing computer programs since 1967 — if my students had someone else do their homework, i'd give them zero marks — learning happens by doing and researching — your peers here at forums.asp.net can help you best by guiding you, not by doing your homework for you.

(a) how did your teacher actually specify the problem?

i've answered the question you've asked ... in the c# world, Decimal means System.Decimal which is a c# data type that is not capable of holding the result of the conversion.

Clarity is important, both in question and in answer.

(b) if you post in other forums you should say so ... for example, someone posted this deleted question on stackoverflow:

[its author removed it voluntarily]

stackoverflow suggests this currently unanswered question as an alternative:
http://stackoverflow.com/questions/960264/convert-really-big-number-from-binary-to-decimal-and-print-it

(c) at what level of school has this question been presented to you?  imho, it requires a very good background to solve this because it's non-trivial.

obviously it's solvable because Taylor Hornby has done it here:  https://defuse.ca/big-number-calculator.htm — however, you would need to store your intermediate and final results in a well designed class instance and display the results as a System.String — this means you'd also need a custom .ToString method.

Hint:  search via Google or your favourite search engine for     algorithms for displaying large powers of two as text — if you get lucky, you will find techniques that you can use to solve you problem.

(d) please restate your homework assignment with clarity  — if necessary, get your teacher to do the same for you first.

(e) be honest with your teacher that you are seeking external help.

BTW, i was a very mean computer science instructor — if a student presented an answer but could not explain it, it was fair to assume that someone else provided that answer and zero marks were likely to be given.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• PatriceSc

All-Star

43720 Points

15301 Posts

### Re: Converting HUGE Binary numbers to Decimal in C# HOW?

Also the solution will depend on what you know (and we don't know what you know). Your teacher may not expect some kind of super advanced solution but rather than you put in practice what you just learned for example maybe an array based solution if you just learned about arrays.

Anyway even when not homework I prefer to explain what to do or how to solve something rather than providing ready to paste code. IMO you learn much more by coding yourself based on some help rather by just copying and pasting code and this is valid even if not at school anymore.

• gerrylowry

Star

14297 Points

5797 Posts

### Re: Converting HUGE Binary numbers to Decimal in C# HOW?

@sham250

building on what PatriceSc wrote above, if you simply just copy and paste code, you will never be a programmer.

Before translating a solution into code, you need to understand the problem in your own language.

Here are more hints:

for any binary number of the general form      ...(1|0)(1|0)(1|0)(1|0)...(1|0)     you only care about the 1's.

starting from the rightmost position, counting up from zero, the value of any is 2^n, where indicates its position.

Example, converting from binary to decimal:

00001001 = 2^3   +  2^0  =  2x2x2    +  1  =  8 + 1  =  9 base 10

For smaller numbers than the one that is part of your homework assignment, you can get away with using .NET Framework types.

```ulong unsignedLong = ulong.MaxValue;
Console.WriteLine (unsignedLong.ToString());

output:

```18446744073709551615
ffffffffffffffff```

```ffffffffffffffff
1111111111111111111111111111111111111111111111111111111111111111```

Think of types like Decimal, Int64, et cetera as being like buckets that can only hold so much; if you have more than one bucket can hold, you need more buckets:
[ffffffffffffffff]  [ffffffffffffffff]

Just like adding real columns of numbers, you need to be able to carry into the next column if necessary:
[ffffffffffffffff]  [ffffffffffffffff] is

```+ ffffffffffffffff0000000000000000
ffffffffffffffff
--------------------------------
= ffffffffffffffffffffffffffffffff
================================```

sham250, your peers at forums.asp.net are still waiting to hear from you.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• sham25

None

0 Points

3 Posts

### Re: Converting HUGE Binary numbers to Decimal in C# HOW?

You can check my stack overflow post here first of all: http://stackoverflow.com/questions/32646201/having-trouble-incorporating-algorithm-pseudocode-any-help-would-be-great?noredirect=1#comment53140060_32646201

• gerrylowry

Star

14297 Points

5797 Posts

### Re: Converting HUGE Binary numbers to Decimal in C# HOW?

First, i respect you for your tenacity ... i suspect that many students who fail an assignment would want to move on and forget all about.

Second, when a teacher demands a specific method from his/her students, it's often for the benefit of the teacher (it's easier to mark one solution 30 times than it is to mark 30 unique solutions); such pedagogy stifles creativity.

There is a story about seven year old mathematician Gauss creating the formula n(n+1)/2 when his elementary school teacher assigned "busy work" to the class by asking the students to sum the numbers 1 to 100.  http://www-history.mcs.st-and.ac.uk/Biographies/Gauss.html

TIMTOWTDI

working with what you've presented here http://stackoverflow.com/questions/32646201/having-trouble-incorporating-algorithm-pseudocode-any-help-would-be-great, i'm going to pick it apart (and later try to handle it properly).

```function ItBin2dec(v)
Input: An n-bit integer v ≥ 0 (binary digits)
Output: The vector w of decimal digits of v
w = []
for i =size(v) − 1 downto 0:
w =By2inDec(w)
if v is odd:
if w = []: w = [1]
else: w[0] = w[0] + 1

return w```

function ItBin2dec(v)

(a) the It part of the function name tells me nothing, so i'm going to have to ignore the first 2 letters.

(b) Bin2dec appears to be a lazy way of writing binary to decimal and implies a converision from base 2 to base 10.

(c) (v) suggests that the function is being passed a binary number.

(d) "Input: An n-bit integer v ≥ 0 (binary digits)" ... this imho is a sloppy description of the input:
https://en.wikipedia.org/wiki/Integer
https://en.wikipedia.org/wiki/Integer_(computer_science)

FWIW, an algorithm is likely to be affected by data type; since your teacher wants a arbitrary number of binary digits (n-bit) and since today's hardware based native number formats that can be directly handled by the processor are not of arbitrary length, a better statement would have been:

`Input: an arbitrary string of length n, n > 0,consisting only of the characters '1' and '0'where the rightmost character represents base 2 position 2^0and the leftmost character represents the base 2 position 2^(n-1);only unsigned binary numbers are represented;the base 10 value of any non-zero position is its power of 2 value. `

(e) "Output: The vector w of decimal digits of v" ...  this imho is a sloppy description of the output.
http://www.oxforddictionaries.com/definition/english/vector

`Output:  the value of v expressed as in base 10.`

(f) "The algorithm By2inDec(z) takes as input the vector of decimal digits of an integer z and returns the vector of decimal digits of 2z."
Again your teacher is abusing terms like "vector" and "decimal digits".

it appears that your teacher is trying to say "for any integer z, return z+z (i.e. 2 times z)

(g) i'm going to attempt to interpret the following line by line:

```w = []
for i =size(v) − 1 downto 0:
w =By2inDec(w)
if v is odd:
if w = []: w = [1]
else: w[0] = w[0] + 1```
```w = []
// define w as a _____```
```for i =size(v) − 1 downto 0:
// interate over v, using as an index the length of v less 1
// counting down inclusive of the zero position.```

this introduces a problem ... experience tells us that i is likely an index, however i is never used in the rest of the enclosed code:

```    w =By2inDec(w)
if v is odd:
if w = []: w = [1]
else: w[0] = w[0] + 1```

the above has a second problem:  w appears to be unassigned on the first iteration:

```w = []
-------
w =By2inDec(w) // ergo w =By2inDec([])  ```
```    if v is odd:
// which 'v'?    v[i]  ???```

i'm going to ignore these two lines:

```        if w = []: w = [1]
else: w[0] = w[0] + 1```

--------------------------------------------------------
Moving on.

sham25 ... i do not see the relevance of this:  https://en.wikipedia.org/wiki/Double_dabble

your example http://stackoverflow.com/questions/32646201/having-trouble-incorporating-algorithm-pseudocode-any-help-would-be-great does not even mention it.

Here's my attempt at a more reasonable pseudo-code:

```public static String ArbitraryBinaryConvertToDecimal(String onesAndZeros)

// c#:  the leftmost character is index value zero
// c#:  the rightmost character is index value (string length minus 1)

the input string must contain only ones and zeros and
is of an arbitrary length > 0;
else throw an exception.

the input string represents an unsigned binary number.

return value:  the base 10 equivalent of the input string.

String currentPowerOf2 = "1"; // zero
String totalDecimalValue = String.Empty;

starting with the rightmost character,
moving left until the index zero character has been processed:
{
if onesAndZeros[index] = '1'
{
`public static String AddTwoBase10Strings(String firstString, String SecondString)`
`public static String AddBase10StringToItself(String StringToBeDoubled)`