Convert.ToString(숫자, 2).PadLeft(8, ‘0’);
예를 들어 다음 샘플 코드처럼 사용할 수 있습니다.
> Convert.ToString(5, 2).PadLeft(4, '0')
"0101"
그럼 Convert.ToString().PadLeft()를 사용해서 정수를 이진수 문자열로 표현해 볼까요?
정수를 이진수 문자열로 표현: BinaryString.cs
using System;
class BinaryString
{
static void Main()
{
byte x = 10; //0000 1010
Console.WriteLine(
$"십진수 : {x} -> 이진수 : {Convert.ToString(x, 2).PadLeft(8, '0')}");
}
}