Byte 데이터 형식 (Visual Basic)Byte data type (Visual Basic)
0에서 255 사이의 값 범위에 해당 하는 부호 없는 8 비트 (1 바이트) 정수를 저장 합니다.Holds unsigned 8-bit (1-byte) integers that range in value from 0 through 255.
설명Remarks
Byte
데이터 형식을 사용 하 여 이진 데이터를 포함 합니다.Use the Byte
data type to contain binary data.
Byte
의 기본값은 0입니다.The default value of Byte
is 0.
리터럴 할당Literal assignments
Byte
10 진수 리터럴, 16 진수 리터럴, 8 진수 리터럴 또는 (Visual Basic 2017부터) 이진 리터럴을 할당 하 여 변수를 선언 하 고 초기화할 수 있습니다.You can declare and initialize a Byte
variable by assigning it a decimal literal, a hexadecimal literal, an octal literal, or (starting with Visual Basic 2017) a binary literal. 정수 리터럴이의 범위 밖에 있는 경우 (즉 보다 Byte
작거나 보다 Byte.MinValue 큰 경우 Byte.MaxValue ) 컴파일 오류가 발생 합니다.If the integral literal is outside the range of a Byte
(that is, if it is less than Byte.MinValue or greater than Byte.MaxValue), a compilation error occurs.
다음 예제에서는 10 진수, 16 진수 및 이진 리터럴로 표현 된 201와 동일한 정수를 암시적으로 정수 에서 값으로 변환 byte
합니다.In the following example, integers equal to 201 that are represented as decimal, hexadecimal, and binary literals are implicitly converted from Integer to byte
values.
Dim byteValue1 As Byte = 201
Console.WriteLine(byteValue1)
Dim byteValue2 As Byte = &H00C9
Console.WriteLine(byteValue2)
Dim byteValue3 As Byte = &B1100_1001
Console.WriteLine(byteValue3)
' The example displays the following output:
' 201
' 201
' 201
참고
접두사 또는를 사용 하 여 &h
&H
16 진수 리터럴을 표시 하거나, 접두사 또는을 사용 하 여 이진 리터럴을 표시 하 고, 접두사 또는를 사용 하 여 &b
&B
&o
&O
8 진수 리터럴을 나타냅니다.You use the prefix &h
or &H
to denote a hexadecimal literal, the prefix &b
or &B
to denote a binary literal, and the prefix &o
or &O
to denote an octal literal. 10진수 리터럴에는 접두사가 없습니다.Decimal literals have no prefix.
Visual Basic 2017부터 _
다음 예제와 같이 밑줄 문자를 자릿수 구분 기호로 사용 하 여 가독성을 높일 수도 있습니다.Starting with Visual Basic 2017, you can also use the underscore character, _
, as a digit separator to enhance readability, as the following example shows.
Dim byteValue3 As Byte = &B1100_1001
Console.WriteLine(byteValue3)
' The example displays the following output:
' 201
Visual Basic 15.5부터 _
접두사와 16 진수, 이진 또는 8 진수 숫자 사이의 선행 구분 기호로 밑줄 문자 ()를 사용할 수도 있습니다.Starting with Visual Basic 15.5, you can also use the underscore character (_
) as a leading separator between the prefix and the hexadecimal, binary, or octal digits. 예를 들면 다음과 같습니다.For example:
Dim number As Byte = &H_6A
선행 구분 기호로 밑줄 문자를 사용하려면 Visual Basic 프로젝트(*.vbproj) 파일에 다음 요소를 추가해야 합니다.To use the underscore character as a leading separator, you must add the following element to your Visual Basic project (*.vbproj) file:
<PropertyGroup>
<LangVersion>15.5</LangVersion>
</PropertyGroup>
자세한 내용은 Visual Basic 언어 버전 설정을참조하십시오.For more information see setting the Visual Basic language version.
프로그래밍 팁Programming tips
음수.Negative Numbers.
Byte
는 부호 없는 형식이 기 때문에 음수를 나타낼 수 없습니다.BecauseByte
is an unsigned type, it cannot represent a negative number.-
형식을 반환 하는 식에 단항 빼기 () 연산자를 사용 하는 경우Byte
Visual Basic는 식을Short
먼저 변환 합니다.If you use the unary minus (-
) operator on an expression that evaluates to typeByte
, Visual Basic converts the expression toShort
first.형식 변환.Format Conversions. Visual Basic 파일을 읽거나 쓸 때 또는 Dll, 메서드 및 속성을 호출 하는 경우 데이터 형식 간에 자동으로 변환할 수 있습니다.When Visual Basic reads or writes files, or when it calls DLLs, methods, and properties, it can automatically convert between data formats. 변수 및 배열에 저장 된 이진 데이터
Byte
는 이러한 형식 변환 중에 보존 됩니다.Binary data stored inByte
variables and arrays is preserved during such format conversions.String
ANSI 및 유니코드 형식 간에 변환 하는 동안 해당 내용이 손상 될 수 있으므로 이진 데이터에 대해 변수를 사용 하면 안 됩니다.You should not use aString
variable for binary data, because its contents can be corrupted during conversion between ANSI and Unicode formats.넓혀.Widening.
Byte
데이터 형식은,,,,,,, 또는로 확대 변환Short
UShort
Integer
UInteger
Long
ULong
Decimal
Single
Double
됩니다.TheByte
data type widens toShort
,UShort
,Integer
,UInteger
,Long
,ULong
,Decimal
,Single
, orDouble
. 즉,Byte
오류가 발생 하지 않고 이러한 형식으로 변환할 수 있습니다 System.OverflowException .This means you can convertByte
to any of these types without encountering a System.OverflowException error.문자를 입력 합니다.Type Characters.
Byte
에는 리터럴 형식 문자 또는 식별자 형식 문자가 없습니다.Byte
has no literal type character or identifier type character.Framework 형식.Framework Type. .NET Framework에서 해당하는 형식은 System.Byte 구조체입니다.The corresponding type in the .NET Framework is the System.Byte structure.
예제Example
다음 예에서는 b
가 Byte
변수입니다.In the following example, b
is a Byte
variable. 문은 변수의 범위와 비트 시프트 연산자의 응용 프로그램을 보여 줍니다.The statements demonstrate the range of the variable and the application of bit-shift operators to it.
' 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)