Byte 데이터 형식(Visual Basic)

0에서 255까지의 값 범위에 있는 부호 없는 8비트(1바이트) 정수를 저장합니다.

설명

이진 데이터를 저장하려면 Byte 데이터 형식을 사용합니다.

Byte의 기본값은 0입니다.

프로그래밍 팁

  • 음수. Byte는 부호 없는 형식이므로 음수를 나타낼 수 없습니다. Byte 형식으로 계산되는 식에서 단항 마이너스(-) 연산자를 사용하면 Visual Basic은 먼저 식을 Short로 변환합니다.

  • 형식 변환. Visual Basic에서 파일을 읽거나 쓰는 경우 또는 DLL, 메서드 및 속성을 호출하는 경우 데이터 형식 간 변환이 자동으로 이루어질 수 있습니다. Byte 변수와 배열에 저장된 이진 데이터는 그러한 형식 변환이 이루어지는 동안 보존됩니다. ANSI와 유니코드 형식 사이에서 변환하는 동안 이진 데이터의 내용이 손상될 수도 있기 때문에 이진 데이터에 String 변수를 사용해서는 안 됩니다.

  • 확대 변환. Byte 데이터 형식은 Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single 또는 Double로 확대 변환됩니다. 이것은 System.OverflowException 오류의 발생 없이 Byte를 이러한 형식 중 하나로 변환할 수 있음을 의미합니다.

  • 형식 문자. Byte에는 리터럴 형식 문자나 식별자 형식 문자가 없습니다.

  • Framework 형식. .NET Framework에서 해당하는 형식은 System.Byte 구조체입니다.

예제

다음 예제에서 b는 Byte 변수입니다. 예제의 문에서는 이 변수의 범위와 비트 시프트 연산자가 적용되는 방법을 보여 줍니다.

' The valid range of a Byte variable is 0 through 255.
Dim b As Byte
b = 30
' The following statement causes an error because the value is too large.
'b = 256
' The following statement causes an error because the value is negative.
'b = -5
' The following statement sets b to 6.
b = CByte(5.7)

' The following statements apply bit-shift operators to b.
' The initial value of b is 6.
Console.WriteLine(b)
' Bit shift to the right divides the number in half. In this 
' example, binary 110 becomes 11.
b >>= 1
' The following statement displays 3.
Console.WriteLine(b)
' Now shift back to the original position, and then one more bit
' to the left. Each shift to the left doubles the value. In this
' example, binary 11 becomes 1100.
b <<= 2
' The following statement displays 12.
Console.WriteLine(b)

참고 항목

참조

데이터 형식 요약(Visual Basic)

System.Byte

형식 변환 함수(Visual Basic)

변환 요약(Visual Basic)

개념

데이터 형식의 효율적 사용(Visual Basic)