Share via


Tipo de dados Byte (Visual Basic)

Holds unsigned 8-bit (1-byte) integers that range in value from 0 through 255.

Comentários

Use o Byte tipo de dados para conter dados binário .

The default value of Byte is 0.

Programming Tips

  • Negative Numbers. Porque Byte é um tipo não assinado, não pode representar um número negativo. If you use the unary minus (-) operator on an expression that evaluates to type Byte, Visual Basic converts the expression to Short first.

  • Formatar Conversões. Quando Visual Basic lê ou grava arquivos, ou quando ele chama DLLs, métodos e propriedades, ele pode automaticamente converter entre formatos de dados. Dados binários armazenados em Byte variáveis e matrizes é preservado durante tais conversões de formato. Você não deve usar um String variável para dados binário , porque seu conteúdo pode estar corrompido durante a conversão entre ANSI e Unicode formatos.

  • Alargamento. The Byte tipo de dados widens to Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single, or Double. This means you can convert Byte to any of these types without encountering a System.OverflowException error.

  • Tipo Caracteres. Byte tem nenhum caractere de tipo literal ou identificador tipo de caractere.

  • Framework Type. O tipo correspondente na.NET Framework é o System.Byte estrutura.

Exemplo

No exemplo a seguir, b é um Byte variável. As declarações de demonstram o intervalo da variável e o aplicativo de operadores bit -deslocar -lo.

' 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)

Consulte também

Referência

Resumo de tipo de dados (Visual Basic)

System.Byte

Funções de conversão de tipo (Visual Basic)

Resumo de conversão (Visual Basic)

Conceitos

Uso eficiente de tipos de dados (Visual Basic)