Advertisement
ตัวอย่างการเขียนโปรแกรมแปลงเลขฐานสองเป็นเลขฐานสิบ โดยใช้ภาษา C# เขียนในรูปแบบของ Console Application และสามารถนำไปประยุกต์ใช้ในการเขียนโปรแกรมในรูปแบบอื่น ๆ ได้ เช่น การเขียนแบบ Windows Forms Application , การเขียนโดยใช้ภาษาอื่น ๆ นอกเหนือจาก C#
Advertisement
/*
* DekDEV.com
*/
using System;
namespace Bin2Dec
{
class Program
{
static void Main(string[] args)
{
Console.Write("\nEnter Binary number : ");
string bin = Console.ReadLine();
Console.WriteLine("\nBinary number is " + BinToDec(bin));
Console.WriteLine("\n\nPowered by dekdev.com");
Console.Read();
}
static double BinToDec(string bin)
{
int i = (bin.Length - 1);
double sum = 0, j = 0;
while (i >= 0)
{
sum += Convert.ToDouble(bin.Substring(i, 1)) * Convert.ToDouble(Math.Pow(2.00, Convert.ToDouble(j)));
j++;
i--;
}
return sum;
}
}
}

Advertisement