Share via


Byte 資料型別 (Visual Basic)

存放不帶正負號的 8 位元 (1 位元組) 整數,範圍從 0 到 255。

備註

使用 Byte 資料型別來包含二進位資料。

Byte 的預設值為 0。

程式設計提示

  • 負數: 由於 Byte 是不帶正負號的型別,故無法代表負數。 如果您在評估為 Byte 型別的運算式中使用一元 (Unary) 減號 (-) 運算子,則 Visual Basic 會先將運算式轉換為 Short。

  • 格式轉換 :當 Visual Basic 讀取或寫入檔案,或呼叫 DLL、方法和屬性時,它可以在資料格式之間自動轉換。 儲存在 Byte 變數及陣列的二進位資料會在格式轉換時保留。 二進位資料不應使用 String 變數,因為資料內容可能會在 ANSI 和 Unicode 格式之間轉換時損毀。

  • 擴展: Byte 資料型別可擴展至 Short、UShort、Integer、UInteger、Long、ULong、Decimal、Single 或 Double。 這表示您可以將 Byte 轉換成這些類型的任何一項,而不會發生 System.OverflowException 錯誤。

  • 型別字元:Byte 沒有常值型別字元或識別項型別字元。

  • 架構型別。 在 .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)