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.


使用Byte包含二進位資料的資料類型。Use the Byte data type to contain binary data.

Byte 的預設值為 0。The default value of Byte is 0.

常值的指派Literal assignments

您可以宣告並初始化Byte變數指派十進位常值、 十六進位常值、 八進位的常值,或 (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.

在下列範例中,以十進位、 十六進位表示的 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

Dim byteValue2 As Byte = &H00C9

Dim byteValue3 As Byte = &B1100_1001
' The example displays the following output:
'          201
'          201
'          201


使用前置詞&h或是&H來表示十六進位常值前置詞&b&B代表二進位常值,以及前置詞&o&O代表八進位的常值。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. 十進位常值沒有前置詞。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
' The example displays the following output:
'          201

從 Visual Basic 15.5 開始,您也可以使用底線字元 (_) 作為前置分隔符號之間的前置詞和十六進位、 二進位或八進位數字。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:


如需詳細資訊,請參閱設定的 Visual Basic 語言版本For more information see setting the Visual Basic language version.

程式設計提示Programming tips

  • 負數的數字。Negative Numbers. 因為Byte是不帶正負號的型別,它無法表示負數。Because Byte 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 type Byte, Visual Basic converts the expression to Short 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 in Byte variables and arrays is preserved during such format conversions. 您不應該使用String變數的二進位資料,因為其內容可能會損毀,ANSI 和 Unicode 格式之間轉換時。You should not use a String variable for binary data, because its contents can be corrupted during conversion between ANSI and Unicode formats.

  • 擴展。Widening. Byte資料類型可擴展為ShortUShortIntegerUIntegerLongULongDecimalSingle,或DoubleThe Byte data type widens to Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single, or Double. 這表示您可以將轉換Byte任何一種類型,而不會發生System.OverflowException時發生錯誤。This means you can convert Byte 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.


在下列範例中,bByte變數。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.
' Bit shift to the right divides the number in half. In this 
' example, binary 110 becomes 11.
b >>= 1
' The following statement displays 3.
' 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.

另請參閱See also