Zengetsu
Zengetsu

Reputation: 109

convert string to integer array C#

I'm a C# newbie learning how to work with arrays. I wrote a small console app that converts binary numbers to their decimal equivalents; however, the sytax I've used seems to be causing the app to - at some point - use the unicode designation of integers instead of the true value of the integer itself, so 1 becomes 49, and 0 becomes 48.

How can I write the app differently to avoid this? Thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Key in binary number and press Enter to calculate decimal equivalent");
            string inputString = Console.ReadLine();

            ////This is supposed to change the user input into character array  - possible issue here
            char[] digitalState = inputString.ToArray();


            int exponent = 0;
            int numberBase = 2;

            int digitIndex = inputString.Length - 1;
            int decimalValue = 0;
            int intermediateValue = 0;

            //Calculates the decimal value of each binary digit by raising two to the power of the position of the digit. The result is then multiplied by the binary digit (i.e. 1 or 0, the "digitalState") to determine whether the result should be accumulated into the final result for the binary number as a whole ("decimalValue").

            while (digitIndex > 0 || digitIndex == 0)
            {

                intermediateValue = (int)Math.Pow(numberBase, exponent) * digitalState[digitIndex]; //The calculation here gives the wrong result, possibly because of the unicode designation vs. true value issue
                decimalValue = decimalValue + intermediateValue;


                digitIndex--;
                exponent++;


            }

            Console.WriteLine("The decimal equivalent of {0} is {1}", inputString, intermediateValue);
            Console.ReadLine();


        }
    }
}

Upvotes: 1

Views: 636

Answers (3)

Falco Alexander
Falco Alexander

Reputation: 3332

there were two errors: you missed the "-48" and wrote the intermediate instead of the result (last line). Not sure how to unline some parts in the codeblock ;)

intermediateValue = (int)Math.Pow(numberBase, exponent) * (digitalState[digitIndex]-48; 
//The calculation here gives the wrong result, 
//possibly because of the unicode designation vs. true value issue
decimalValue += intermediateValue;
(.....)
Console.WriteLine("The decimal equivalent of {0} is {1}", inputString, decimalValue);

Upvotes: 0

Richard Schneider
Richard Schneider

Reputation: 35477

@CharlesMager says it all. However, I assume this is a homework assignment. So as you said multiplying by the ASCII value is wrong. So just subtract '0' (decimal value 48) from ASCII value.

intermediateValue = (int)Math.Pow(numberBase, exponent) 
  * ((int)digitalState[digitIndex] - 48); 

You code is ugly, there is no need to go backwards from the string. Also using Math.Power is inefficient, shifting (<<) is equivalent for binary powers.

 long v = 0;
 foreach (var c in inputString)
 {
    v = (v << 1) + ((int)c - 48);
 } 
 Console.WriteLine("The decimal equivalent of {0} is {1}", inputString, v);
 Console.ReadLine();

Upvotes: 0

Hamed
Hamed

Reputation: 1213

Simply use the following code

for (int i = 0; i < digitalState.Length; i++)
{
    digitalState[i] = (char)(digitalState[i] - 48);
}

After

char[] digitalState = inputString.ToArray();

Note that the value of a character, for example '1' is different from what it represents. As you already noticed '1' is equal to ASCII code 49. When you subtract 48 from its value (49) it becomes 1.

Upvotes: 1

Related Questions