Converting HUGE Binary numbers to Decimal in C# HOW?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1398 Views - Last Post: 18 September 2015 - 04:48 AM Rate Topic: -----

#1 Eshua   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 11-May 14

Converting HUGE Binary numbers to Decimal in C# HOW?

Posted 16 September 2015 - 09:34 PM

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.

Here is what I had originally, but apparently I am not allowed to use
BigInteger 
so I'm not sure how to store this massive number im going to get..

       //Variable to store the input
                string input = "";
    
                //Variables to do conversions
                BigInteger base2 = 1;
                BigInteger decimalOutput = 0;
    
                //Base k
                int base_K;
    
                //Prompt User
                Console.WriteLine("Enter the Binary Number you would like to convert to decimal: ");
                input = Console.ReadLine();
    
                //Store the user input in a vector
                for (int i = 0; i < input.Length; i++)
                {
                    //If we find a 0, store it in the appropriate vector, otherwise we found a 1..
                    if (input[i].Equals('0'))
                    {
                        binaryVector.Add(0);
                    }
                    else
                    {
                        binaryVector.Add(1);
                    }
                }
    
                //Reverse the vector
                binaryVector.Reverse();
    
                //Convert the Binary # to Decimal
                for (int i = 0; i < binaryVector.Count; i++)
                {
                    //0101 For Example: 0 + (0*1) = 0 Thus: 0 is out current Decimal
                    //While our base2 variable is now a multiple of 2 (1 * 2 = 2)..
                    decimalOutput = decimalOutput + (binaryVector[i] * base2);
                    base2 = base2 * 2;
                }
    
                //Convert Decimal Output to String
                string tempString = decimalOutput.ToString();
                Console.WriteLine("Equivalent Decimal is: " + tempString);



Is This A Good Question/Topic? 0
  • +

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

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

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

Posted 17 September 2015 - 03:40 AM

Quote

but apparently I am not allowed to use

Who is imposing this limitation on you?

Also, I'm just personally curious where these massive numbers are coming from. Even Donal Trump's bank account as described in pennies isn't this big.

This post has been edited by tlhIn`toq: 17 September 2015 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6333
  • View blog
  • Posts: 21,745
  • Joined: 05-May 12

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

Posted 17 September 2015 - 06:09 AM

And the other question is do you need to convert from binary to decimal, or binary to floating point? Remember that in C#, decimal is another data type, and it has its own binary encoding that is different from the IEEE floating point numbers.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6333
  • View blog
  • Posts: 21,745
  • Joined: 05-May 12

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

Posted 17 September 2015 - 06:44 AM

After more thought, this seems like a variation on the standard assignment of showing the base 10 representation of a binary number, but it forces the student to do most of the hard work doing the computations instead of just putting the binary pattern into a variable/class and then having it spit out the based 10 value.

Anyway, see my post in the Computer Science forum. I give a link a an algorithm to do this efficiently.
Was This Post Helpful? 1
  • +
  • -

#5 Eshua   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 11-May 14

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

Posted 17 September 2015 - 08:10 AM

I've read the WIKI page. I don't understand what's going on if I'm being honest here... My professor gave me this function pseudocode
function bin2dec(v)
Input: An n-bit integer v ≥ 0 (binary digits)
Output: The vector of decimal digits of v
if v = []: return w = []
z =bin2dec(bv/2c)
w =By2inDec(z)
if v is odd:
if w = []: w = [1]
else: w[0] = w[0] + 1
return w



But I'm not really sure how to implement this .. I am using C# by the way.
Here is what I've wrote so far...Which hardly makes sense.. Again thanks for taking the time to listen.

static void Main(string[] args)
        {
            //Declare a vector to hold user's current binary # (v)
            List<int> binaryInput = new List<int>();

            string input = "";
            input = Console.ReadLine();

            Console.ReadKey();
        }

        static int By2inDec(int currentDigit)
        {
            //Not sure what to do here?
            return currentDigit * 2;
        }

        static List<int> bin2dec(List<int> v)
        {
            //w 
            List<int> w = new List<int>();

            for(int i = (v.Count - 1); i > 0; i--)
            {
                //Is this possibly what they want? But w is empty... ?
                w.Add(By2inDec(w[i]));

                //If its odd
                if(v[i] % 2 == 0)
                {
                    if (w.Count == 0)
                    {
                        w.Add(1);
                    }
                    else
                    {
                        w[0] = w[0] + 1;
                    }
                }
            }
            return w;
        }

Was This Post Helpful? 0
  • +
  • -

#6 Eshua   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 11-May 14

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

Posted 17 September 2015 - 09:10 AM

EDIT: Line 5 of the pseudocode should read:
z =bin2dec(floorFunction(v/2))

Was This Post Helpful? 0
  • +
  • -

#7 Eshua   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 11-May 14

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

Posted 17 September 2015 - 09:54 AM

Upon further research, I've found that this double dabble algorithm implies that I shift to the left 1 every time to get my binary representation to some kind of "Binary Coded Decimal". I don't understand how to go about that though. I've searched for hours for just one example of the double dabble in C# unfortunately to no avail. You all are obviously seasoned vets of the program game, can I ask you this: What exactly is the real life application of what my professor is having me do? Why the headache? Why do I need to know how to do this when there are tons of DLL, as well as Data Types created specifically for this purpose? I'm just curious if I'm missing the big picture here.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14243
  • View blog
  • Posts: 57,137
  • Joined: 12-June 08

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

Posted 17 September 2015 - 09:58 AM

Have you asked your professor that?

Complex problem solving exercises a multifaceted mental tool set. At the bare minimum this would be a main reason.
Was This Post Helpful? 1
  • +
  • -

#9 Eshua   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 11-May 14

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

Posted 17 September 2015 - 10:01 AM

View Postmodi123_1, on 17 September 2015 - 09:58 AM, said:

Have you asked your professor that?

Complex problem solving exercises a multifaceted mental tool set. At the bare minimum this would be a main reason.


Fair enough. I guess the ability to solve problems is always good.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

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

Posted 17 September 2015 - 10:20 AM

Quote

Why do I need to know how to do this when there are tons of DLL, as well as Data Types created specifically for this purpose?


What are your aspirations in life as a developer? Do you want to be the guy that scripts together everyone else's work? Or do you want to be the guy that creates the DLL's and knows how to make their own Data Type? Do you want to be the code monkey on the team, or do you want to be a team leader and architect handing out the assignments to the code monkeys?
Was This Post Helpful? 1
  • +
  • -

#11 Eshua   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 11-May 14

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

Posted 17 September 2015 - 10:32 AM

View PosttlhIn`toq, on 17 September 2015 - 10:20 AM, said:

Quote

Why do I need to know how to do this when there are tons of DLL, as well as Data Types created specifically for this purpose?


What are your aspirations in life as a developer? Do you want to be the guy that scripts together everyone else's work? Or do you want to be the guy that creates the DLL's and knows how to make their own Data Type? Do you want to be the code monkey on the team, or do you want to be a team leader and architect handing out the assignments to the code monkeys?


+1. Good point.

With that being said, can anyone give me a hint into how to start this algorithm? Assuming I put my binary number in a string, I'm not sure how I can shift a string since it is a array of characters, also I'm not sure how I can use the <<1 shift function on a integer since it is an integer and thus not a binary representation.
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

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

Posted 17 September 2015 - 10:44 AM

Quote

What exactly is the real life application of what my professor is having me do? Why the headache? Why do I need to know how to do this when there are tons of DLL, as well as Data Types created specifically for this purpose? I'm just curious if I'm missing the big picture here.


I'll weigh in on this too: you'll do this a lot while learning. You'll be told to implement something that already exists (and is probably better coded than anything you or I would come up with). I wondered why at first too. Why bother teaching us sorting algorithms, when the built in .Sort functions use more efficient algorithms than they're teaching us? Why learn how to build a linked list when there's a LinkedList<T> class alread? That kind of thing.

The answer is, "if you know how it works, you can make smart decisions about it". When they teach you to implement a linked list, they're not expecting you to roll your own every time you decide to use one. They're teaching you how the insides work, and the best way to learn that is to implement it. And knowing how it works, you can decide if you should use a LinkedList or maybe some other type of collection. You know it's strengths and weaknesses. Plus, it's a good exercise to practice general programming.

Same here. This isn't because you'll ever need to do this yourself. You need to know the math involved.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6333
  • View blog
  • Posts: 21,745
  • Joined: 05-May 12

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

Posted 17 September 2015 - 06:26 PM

And to bring this back into topic...

View PostEshua, on 17 September 2015 - 12:54 PM, said:

Upon further research, I've found that this double dabble algorithm implies that I shift to the left 1 every time to get my binary representation to some kind of "Binary Coded Decimal". I don't understand how to go about that though. I've searched for hours for just one example of the double dabble in C# unfortunately to no avail.


Let's look at a simple array. If you had the following items:
[ 1, 2, 3, 4 ]


and you shift it left once, you should get:
[ 2, 3, 4, 4 ]



If you shift it left again, you should get:
[ 3, 4, 4, 4 ]



I'll leave it to you to figure how to do that kind of array shifting since it'll be good for you. :)/>

Now as for a BCD or (Binary Coded Decimal), consider the the following binary pattern in byte:
0001 1001
   1    9    - hexadecimal



Normally, we would say that byte contains the value of 25 (decimal) or 0x19 (hexadecimal). But wouldn't it be nice if we could treat each nibble as if it were a decimal digit --> a binary coded decimal digit? If we did treated that byte as a BCD, then we would say it contains the value of 19 (decimal).

So let's go back to saying that we have a binary value of 00001101. It has a decimal value of 13 (decimal), or 0x0E (hexadecimal).
0000 1101
   0    D



By using the double dabble algorithm, we can end up with the following pattern:
0001 0011
   1    3


Now that is a more useful form if wanted to print out decimal digits.
Was This Post Helpful? 1
  • +
  • -

#14 Eshua   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 11-May 14

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

Posted 17 September 2015 - 06:30 PM

So basically what I need to do is:

store my input in some type of string. Then split my input into divisions of 4, and if one does not have 4 digits in its division then add 00s to it to make it a 4 digit binary representation... Then I need to look and say hey is this 4 digit binary representation greater than 5? If it yes then SHIFT left until it is not... Am I even close here?

This post has been edited by Eshua: 17 September 2015 - 06:32 PM

Was This Post Helpful? 0
  • +
  • -

#15 Eshua   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 11-May 14

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

Posted 17 September 2015 - 08:39 PM

Trying to mimmick my teachers pseudo code that he provided which is:
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


I have come up with:

static void Main(string[] args)
        {
            List<int> binaryInput = new List<int>();
            List<int> testOutput = new List<int>();
            string input = Console.ReadLine();
            for(int i = 0; i < input.Length; i++)
            {
                binaryInput.Add(Convert.ToInt32(input[i]));
            }
            testOutput = BinaryToDecimal(binaryInput);
            for(int i = 0; i < testOutput.Count; i ++)
            {
                Console.Write(testOutput[i]);
            }
            Console.ReadKey();

        }

        static List<int> BinaryToDecimal(List<int> pInput)
        {
            List<int> decimalOutput = new List<int>();
            for (int i = (pInput.Count-1); i > 0; i--)
            {
                decimalOutput = by2InDec(decimalOutput);
                if (pInput[i] % 2 != 0)
                {
                    if (decimalOutput.Count == 0)
                    {
                        decimalOutput.Add(1);
                    }
                    else
                    {
                        decimalOutput[0] = decimalOutput[0] + 1;
                    }
                }
            }
            return decimalOutput;
        }

        static List<int> by2InDec(List<int> pVector)
        {
            int extra = 0;
            int carry = 0;
            List<int> testDummyList = new List<int>();

            if(pVector.Count == 1 && pVector[0] == 0)
            {
                testDummyList.Add(0);
                return testDummyList;
            }

            for(int i = 0; i < pVector.Count; i++)
            {
                extra = (pVector[i] * 2) + carry;

                if(extra >= 10)
                {
                    extra = extra - 10;
                    testDummyList.Add(extra);
                    carry = 1;
                }
                else
                {
                    testDummyList.Add(extra);
                    carry = 0;
                }
                extra = 0;
            }
            if(carry == 1)
            {
                testDummyList.Add(1);
            }
            return testDummyList;
        }



but still can't get my code to work properly. I decided to not use the Double Dabble Alg by the way. This program is driving me crazy.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2