バイト型 (Byte) (Visual Basic)

0 から 255 までの符号なし 8 ビット (1 バイト) の整数です。

解説

Byte データ型はバイナリ データの格納に使用します。

Byte の既定値は 0 です。

プログラミングのヒント

  • **負の数。**Byte 型は符号を持たないので、負の数を表現できません。 バイト型 (Byte) を評価する式で単項マイナス演算子 (-) を使用すると、Visual Basic では、最初に式が Short 型に変換されます。

  • **形式の変換。**Visual Basic がファイルを読み書きするとき、または DLL、メソッド、プロパティを呼び出すとき、データ形式が自動的に変換されます。 バイト型 (Byte) の変数および配列に格納されるバイナリ データは、形式変換中に保存されます。 バイナリ データに文字列型 (String) の変数を使用しないでください。これは、ANSI 形式と Unicode 形式の変換時にデータの内容が破損する場合があるためです。

  • **拡大変換。**Byte データ型は、Short、UShort、Integer、UInteger、Long、ULong、Decimal、Single、または Double に拡大変換されます。 これは、OverflowException エラーを発生させることなく、これらの型のいずれかに Byte を変換できることを意味します。

  • 型宣言文字。 Byte 型には、リテラルの型文字も識別子の型文字もありません。

  • Framework のデータ型。.NET Framework において対応する型は、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)

Byte

データ型変換関数 (Visual Basic)

変換の概要 (Visual Basic)

概念

データ型の有効な使用方法 (Visual Basic)