본문 바로가기
Programming/C# and .NET

C# REF. Notes - 001 - 16진수 데이터 값을 이진수로 출력하기

by The Programmer 2025. 2. 11.

/**

 * C# 언어에서 16진수 데이터 값을 이진수로 출력하는 방법입니다.

 

*/

 

□ 16진수 데이터 값을 이진수로 출력하기

 

// 간단한 참고용이라 그냥.

// 16진수 데이터 값을 이진수로 출력하기

using System;

class Program
{
    static void Main()
    {
        string hexValue = "1A3F"; // 변환할 16진수 값, 문자열 형태로 주어졌을 경우.
        int intValue = Convert.ToInt32(hexValue, 16); // 16진수 문자열을 정수로 변환
        string binaryValue = Convert.ToString(intValue, 2); // 정수를 이진수 문자열로 변환

        Console.WriteLine($"16진수: {hexValue}"); // 16진수 값 출력
        Console.WriteLine($"이진수: {binaryValue}"); // 이진수 값 출력
    }
}

 

 

□ 참고 : 명시적 형변환.

 

int int4Bytes = 0xffff; // 16진수 값 직접 주어졌을 경우. 0xffff에서 ffff의 대문자는 직접 테스트해보세요.

// 16진수(Hexadecimal) 하나가 4비트로 표현될 수 있다. 그러므로 여기서 이 값은 2Bytes만 있어도 됨.

short st2Bytes;

 

st2Bytes = (short) int4Bytes

 

Console.WriteLine("int4Bytes := " + int4Bytes); // 잠자리 눈, Pascal. ^.^;

Console.WriteLine("st2Bytes := " + st2Bytes );

 

 

□ '친절한 설명 따위는 필요 없다' 철학을 '대충' 지키고 있습니다. ^.^;

 

 

즐거운 프로그래밍!

^.^

 

 

 

 

'Programming > C# and .NET' 카테고리의 다른 글

유용한 무료 C# 강좌 - 1  (0) 2024.09.08