Byte data type (Visual Basic)
Holds unsigned 8-bit (1-byte) integers that range in value from 0 through 255.
Byte data type to contain binary data.
The default value of
Byte is 0.
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. 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.
In the following example, integers equal to 201 that are represented as decimal, hexadecimal, and binary literals are implicitly converted from Integer to
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
You use the prefix
&H to denote a hexadecimal literal, the prefix
&B to denote a binary literal, and the prefix
&O to denote an octal literal. Decimal literals have no prefix.
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
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
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>
For more information see setting the Visual Basic language version.
Negative Numbers. Because
Byteis an unsigned type, it cannot represent a negative number. If you use the unary minus (
-) operator on an expression that evaluates to type
Byte, Visual Basic converts the expression to
Format Conversions. When Visual Basic reads or writes files, or when it calls DLLs, methods, and properties, it can automatically convert between data formats. Binary data stored in
Bytevariables and arrays is preserved during such format conversions. You should not use a
Stringvariable for binary data, because its contents can be corrupted during conversion between ANSI and Unicode formats.
Bytedata type widens to
Double. This means you can convert
Byteto any of these types without encountering a System.OverflowException error.
Bytehas no literal type character or identifier type character.
Framework Type. The corresponding type in the .NET Framework is the System.Byte structure.
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)
Send feedback about: