C# 기초 - 정수의 2진법, 10진법, 16진법

2023. 10. 20. 22:18C#/기초

개요

Ⅰ. 개념
  1. 정수의 진수 표현: 2진법, 10진법, 16진법
  2. 정수의 진수 변환 출력
  3. 정수의 진법 변환: Convert.ToString( )


Ⅱ. 코드
  1. 정수의 진수 변환 출력
  2. 정수의 진법 변환

 

 

 Ⅰ. 개념

 

1. 정수의 진수 표현: 2진법, 10진법, 16진법

진법은 수를 표현하는 방식,

진수는 진법에 의해서 표현된 수를 말함

 

진법 표현이나 자릿수는 대문자, 소문자 혼용이 가능함

 

1) 2진법(binary)

int bin = 0B1110; // 또는 0b1110;

자릿수로 사용되는 숫자(2개): 0, 1

앞에 0B 또는 0b를 추가해서 표현함

 

컴퓨터(하드웨어)의 진법

 

2) 10진법(decimal)

int dec = 14;

자릿수로 사용되는 숫자(10개): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

 

프로그래머에게 익숙한 진법

 

3) 16진법(hexadecimal)

int hex = 0XE; // 또는 0xE

자릿수로 사용되는 숫자(16개): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

앞에 0X 또는 0x를 추가해서 표현함

 

프로그래머와 컴퓨터의 중간 진법

 

 

2. 정수의 진수 변환 출력

10진수, 16진수는 명시적 표시가 가능하지만,

2진수는 형식이 따로 없으므로, 프로그램으로 짜야함

 

1) 기본

Console.WriteLine("bin= {0}, dec= {1}, hex= {2}", bin, dec, hex);

기본 출력은 10진수임

 

2) 10진수 명시적 표시

Console.WriteLine("bin= {0:D}, dec= {1:D}, hex= {2:D}", bin, dec, hex);
Console.WriteLine("bin= {0:d}, dec= {1:d}, hex= {2:d}", bin, dec, hex);

D 또는 d를 사용함

 

Console.WriteLine("bin= {0:d4}, dec= {1:d4}, hex= {2:d4}", bin, dec, hex); // 10진수 4자리

뒤에 숫자를 붙여 출력 자릿수를 정할 수 있음

 

3) 16진수 명시적 표시

Console.WriteLine("bin= 0X{0:X}, dec= 0X{1:X}, hex= 0X{2:X}", bin, dec, hex);
Console.WriteLine("bin= 0x{0:x}, dec= 0x{1:x}, hex= 0x{2:x}", bin, dec, hex);

X 또는 x를 사용함 (x를 사용하면 자릿수의 A, B, C, D, E, F도 소문자로 출력됨)

10진수랑 헷갈리지 않도록 숫자 앞에 0X 또는 0x를 붙여서 16진수 임을 나타냄

 

Console.WriteLine("bin= 0x{0:x4}, dec= 0x{1:x4}, hex= 0x{2:x4}", bin, dec, hex); // 16진수 4자리

뒤에 숫자를 붙여 출력 자릿수를 정할 수 있음

 

 

3. 정수의 진법 변환Convert.ToString( )

Convert.ToString(바꾸려는 수, 기수)

변환할 진법을 기수 부분에 넣으면 됨

 

Convert.ToString(바꾸려는 수, 2)  //  2진수
Convert.ToString(바꾸려는 수, 10) // 10진수
Convert.ToString(바꾸려는 수, 16) // 16진수

 

 

 Ⅱ. 코드

 

1. 정수의 진수 변환 출력

1) 코드

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int bin = 0B1110;
            int dec = 14;
            int hex = 0XE;

            Console.WriteLine("---------- 기본 ----------");
            Console.WriteLine("bin= {0}, dec= {1}, hex= {2}", bin, dec, hex);
            Console.WriteLine();

            Console.WriteLine("------- 명시적 표시 -------");
            Console.WriteLine("1) 10진수");
            Console.WriteLine("bin= {0:D}, dec= {1:D}, hex= {2:D}", bin, dec, hex);
            Console.WriteLine("bin= {0:d4}, dec= {1:d4}, hex= {2:d4}", bin, dec, hex);
            Console.WriteLine();

            Console.WriteLine("2) 16진수");
            Console.WriteLine("bin= 0X{0:X}, dec= 0X{1:X}, hex= 0X{2:X}", bin, dec, hex);
            Console.WriteLine("bin= 0x{0:x4}, dec= 0x{1:x4}, hex= 0x{2:x4}", bin, dec, hex);
            Console.WriteLine();
        }
    }
}

 

2) 실행 결과

 

 

2. 정수의 진법 변환

1) 코드

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int dec = 14;

            Console.WriteLine("dec를  2진수로 변환: 0B{0}", Convert.ToString(dec, 2));
            Console.WriteLine("dec를 10진수로 변환: {0}", Convert.ToString(dec, 10));
            Console.WriteLine("dec를 16진수로 변환: 0X{0}", Convert.ToString(dec, 16));
        }
    }
}

 

2) 실행 결과

 

 


[공부 일자] 2023.09.20

'C# > 기초' 카테고리의 다른 글

C# 기초 - [참고] 일반 표기법과 지수 표기법  (0) 2023.10.19
C# 기초 - 수학 클래스 Math  (0) 2023.10.19
C# 기초 - 표준 입출력  (0) 2023.10.16
C# 기초 - C# 프로그래밍  (0) 2023.09.30
C# 기초 - 실습 환경 구축  (0) 2023.09.28