开发者

Binary to decimal

int rem, count = 0;
long int n=0, b, i;

count << "Enter the Binary value to convert in Decimal = ";
cin >> b;
i = b;

while (b > 0)
{
    rem = b % 10;
    n = n + rem * pow(2, count);
    count++;
    b = b / 10;
}

开发者_StackOverflowcout << "The decimal value of Binary no. = " << i << " = " << n;
getch();

I made this simple program in C++ and now I want to implement it in C# but I couldn't do so because I don't know how to implement the logic which I used in the loop. Because in C++ the keyword pow is used to multiply the value of 2 so I don't know how to do it in C#.


No, pow() is not a keyword, it's a function from the standard library's math.h.

You can easily replace it in this case, for both C++ and C#, with bit shifting:

(int) pow(2, count) == 1 << count

The above is true for all positive values of count, up to the limit of the platform's/language's precision.

I believe the problem as a whole is much easier to solve using shifting.


Check this:

int bintodec(int decimal);

int _tmain(int argc, _TCHAR* argv[])
{ 
   int decimal;

   printf("Enter an integer (0's and 1's): ");
   scanf_s("%d", &decimal);

   printf("The decimal equivalent is %d.\n", bintodec(decimal));

   getchar();
   getchar();
   return 0;
}

int bintodec(int decimal)
{
   int total = 0;
   int power = 1;

   while(decimal > 0)
   {
      total += decimal % 10 * power;
      decimal = decimal / 10;
      power = power * 2;
   }

   return total;
}


You have to take care of data types in C#

long int n=0, b, i;  // long int is not valid type in C#, Use only int type.

Replace pow() to Math.Pow()

 pow(2, count);             // pow() is a function in C/C++
 ((int)Math.Pow(2, count))  // Math.Pow() is equivalent of pow in C#. 
                            // Math.Pow() returns a double value, so cast it to int


Have a look at the Math.Pow Method.

In general, the Math class provides much functionality you are looking for.

A complete code example elsewhere on the internet is enter link description here.


    public int BinaryToDecimal(string data)
    {
        int result = 0;
        char[] numbers = data.ToCharArray();

        try
        {
            if (!IsNumeric(data))
                error = "Invalid Value - This is not a numeric value";
            else
            {
                for (int counter = numbers.Length; counter > 0; counter--)
                {
                    if ((numbers[counter - 1].ToString() != "0") && (numbers[counter - 1].ToString() != "1"))
                        error = "Invalid Value - This is not a binary number";
                    else
                    {
                        int num = int.Parse(numbers[counter - 1].ToString());
                        int exp = numbers.Length - counter;
                        result += (Convert.ToInt16(Math.Pow(2, exp)) * num);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            error = ex.Message;
        }
        return result;
    }

http://zamirsblog.blogspot.com/2011/10/convert-binary-to-decimal-in-c.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜