description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | |||
---|---|---|---|---|---|---|---|---|
Learn more about: Byte data type (Visual Basic) |
Byte Data Type |
01/31/2018 |
|
|
eed44dff-eaee-4937-a89f-444e418e74f6 |
Holds unsigned 8-bit (1-byte) integers that range in value from 0 through 255.
Use the 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 xref:System.Byte.MinValue?displayProperty=nameWithType or greater than xref:System.Byte.MaxValue?displayProperty=nameWithType), 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 byte
values.
[!code-vbByte]
Note
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.
Starting with Visual Basic 2017, you can also use the underscore character, _
, as a digit separator to enhance readability, as the following example shows.
[!code-vbByte]
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
[!INCLUDE supporting-underscores]
-
Negative Numbers. Because
Byte
is an unsigned type, it cannot represent a negative number. If you use the unary minus (-
) operator on an expression that evaluates to typeByte
, Visual Basic converts the expression toShort
first. -
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
Byte
variables and arrays is preserved during such format conversions. You should not use aString
variable for binary data, because its contents can be corrupted during conversion between ANSI and Unicode formats. -
Widening. The
Byte
data type widens toShort
,UShort
,Integer
,UInteger
,Long
,ULong
,Decimal
,Single
, orDouble
. This means you can convertByte
to any of these types without encountering a xref:System.OverflowException?displayProperty=nameWithType error. -
Type Characters.
Byte
has no literal type character or identifier type character. -
Framework Type. The corresponding type in the .NET Framework is the xref:System.Byte?displayProperty=nameWithType 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.
[!code-vbVbVbalrDataTypes#16]
- xref:System.Byte?displayProperty=nameWithType
- Data Types
- Type Conversion Functions
- Conversion Summary
- Efficient Use of Data Types