IT/C#
[C#] Bit, Byte, Word 정리. C# 자료형 사이즈 범위 정리. BitConverter 사용 예시.
Ella.J
2024. 10. 3. 21:52
728x90
반응형
[ Bit, Byte, Word 정리 ]
Bit | 일(1)과 영(0) 2개로 표현하는 가장 작은 저장 단위 | |
Byte | 8Bit를 묶어서 사용하는 단위 [1 Byte = 8 Bit] | 2^8 = 256개의 정보 표현 가능 |
Word | Byte를 묶어서 사용하는 단위 | 32비트 컴퓨터는 1 Word = 4 Byte = 32 Bit 64비트 컴퓨터는 1 Word = 8 Byte = 64 Bit |
[ C# 자료형 ]
자료형 | Size | 범위 | |
정수 숫자 형식 | sbyte | 1 Byte | -128 ~ 127 |
byte | 1 Byte | 0 ~ 255 | |
short | 2 Byte | -32,768 ~ 32,767 | |
ushort | 2 Byte | 0 ~ 65,535 | |
int | 4 Byte | -2,147,483,648 ~ 2,147,483,647 | |
uint | 4 Byte | 0 ~ 4,294,967,295 | |
long | 8 Byte | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | |
ulong | 8 Byte | 0 ~ 18,446,744,073,709,551,615 | |
부동 소수점 숫자 형식 | float | 4 Byte | ±1.5 x 10−45 ~ ±3.4 x 1038 |
double | 8 Byte | ±5.0 × 10−324 ~ ±1.7 × 10308 | |
문자 형식 | char | 16 Bit | U+0000~U+FFFF |
논리 형식 | bool | 1 Bit | True(1) 또는 False(0) |
[Short to Bit Array (Bool Array)]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static bool[] ShortToBitArray(short data)
{
//Short(2 Byte) to Byte Array
byte[] bytes = BitConverter.GetBytes(data);
//bytes[0], bytes[1]
//Byte Array to BitArray
BitArray bits = new BitArray(bytes);
//bits.Count = 16 (2 Byte = 16 Bit)
//BitArray copy to Bool Array
bool[] arr = new bool[bits.Length];
bits.CopyTo(arr, 0);
return arr;
}
|
cs |
* byte[] BitConverter.GetBytes(short value)
: value 값을 바이트 배열로 반환. 여기서는 short이니깐 2Byte 반환.
[Byte[] to Int]
1
2
3
4
5
|
public static int ByteArrayToInt(byte[] bytes)
{
//if bytes = {0, 0, 0, 25} => return 25
return BitConverter.ToInt32(bytes, 0);
}
|
cs |
* int BitConverter.ToInt32(byte[] value, int startIndex)
: 바이트 배열(value)에 지정된 위치(startIndex)에서 시작하여
4Byte를 32Bit 부호 있는 정수(Int)로 반환
* int BitConverter.ToInt16(byte[] value, int startIndex)
: 바이트 배열(value)에 지정된 위치(startIndex)에서 시작하여
2Byte를 16Bit 부호 있는 정수(Int)로 반환
[BitConverter 참고]
728x90
반응형