1. 진수변환
"Convert.ToString(값, 진수)"
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
byte decData, binData, hexData;
decData = 14_7;
binData = 0b1001_0011;
hexData = 0x9_e;
Console.WriteLine("10진(decData :" + Convert.ToString(decData, 10));
Console.WriteLine("2진((binData) : " + Convert.ToString(binData, 2));
Console.WriteLine("16진수(hexData) : " + Convert.ToString(hexData, 16));
}
}
}
"Convert.ToInt(문자열, 진수)"
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Convert.ToInt32("12341234", 10));
Console.WriteLine(Convert.ToInt32("11111111", 2));
Console.WriteLine(Convert.ToInt32("FFFF", 16));
}
}
}
#진수변환프로그램_120페이지
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int sel;
string num;
int num10 = 0;
Console.Write("입력 진수 결정(16/10/2) : ");
sel = Convert.ToInt32(Console.ReadLine());
// 16, 10, 2 이외의 값이 입력되었을 경우 종료
if (sel != 16 && sel != 10 && sel != 2)
{
Console.WriteLine("16, 10, 2 숫자 중 하나만 입력하세요");
return; // 프로그램 종료
}
Console.Write("값 입력 : ");
num = Console.ReadLine();
// 선택한 진수에 따라 num을 변환
if (sel == 16)
num10 = Convert.ToInt32(num, 16);
if (sel == 10)
num10 = Convert.ToInt32(num, 10);
if (sel == 2)
num10 = Convert.ToInt32(num, 2);
// 변환된 10진수 값을 다른 진수로 출력
Console.WriteLine("10진수 : " + Convert.ToString(num10, 10));
Console.WriteLine("2진수 : " + Convert.ToString(num10, 2));
Console.WriteLine("16진수 : " + Convert.ToString(num10, 16));
}
}
}