Binary to unsigned decimal - CSharp System

CSharp examples for System:Math Number

Description

Binary to unsigned decimal

Demo Code


using System.Text;
using System.Collections.Generic;
using System;/*from  www .j  ava 2  s.c  o m*/

public class Main{
        /// <summary>
        /// Binary to unsigned decimal
        /// </summary>
        public static long BinToUDec(string input)
        {
            long result = 0;
            byte[] binValue = new byte[input.Length];
            int pow = input.Length - 1;

            // Fill array with 0 and 1
            for (int i = 0; i < input.Length; i++)
            {
                binValue[i] = byte.Parse(input[i].ToString());
            }

            for (int pos = 0; pos < binValue.Length; pos++)
            {
                result += binValue[pos] * Numbers.NumberToPower(2, pow);
                pow--;
            }

            return result;
        }
}

Related Tutorials