728x90
using System;
using System.Threading;
namespace Study
{
class Program
{
static void Main(string[] args)
{
byte[] bytes = { 0, 0, 0, 25 };
if (BitConverter.IsLittleEndian) // 오른쪽 끝에 중요한 게 있다
Array.Reverse(bytes); // 배열을 반대로 정렬 (중요한 걸 왼쪽 끝으로 이동)
int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine($"byte -> int : {i}");
byte[] bytes2 = BitConverter.GetBytes(2687511146);
Console.WriteLine("int -> byte : " + BitConverter.ToString(bytes2));
}
}
}
기본 형식 데이터를 바이트로 변환하거나, 바이트를 기본 형식 데이터로 변환하고자 할 때는 BitConverter를 이용한다. BitConverter 메서드는 아주 많은 종류가 있다. 원하는 종류를 찾아 쓰면 된다.
https://docs.microsoft.com/ko-kr/dotnet/api/system.bitconverter?view=net-6.0
BitConverter 클래스 (System)
기본 데이터 형식을 바이트의 배열로, 바이트의 배열을 기본 데이터 형식으로 변환합니다.
docs.microsoft.com
728x90
'언어 > C#' 카테고리의 다른 글
[C#] Span이란? (0) | 2022.06.07 |
---|---|
[C#] ArraySegment를 이용하여 배열 가져오기 (0) | 2022.06.07 |
[C#] delegate (델리게이트 / 대리자)와 Invoke (0) | 2022.05.31 |
[C#] 이벤트(event)란? (0) | 2022.05.27 |
[C#] Volatile이란? (0) | 2022.05.26 |