Tipo de datos byte (Visual Basic)
Contiene enteros de 8 bits (1 byte) sin signo que oscilan entre 0 y 255.
Comentarios
Use el Byte tipo de datos para contener datos binarios.
El valor predeterminado de Byte es 0.
Asignaciones literales
Puede declarar e inicializar una variable asignándose un literal decimal, un literal hexadecimal, un literal octal o (a partir Byte de Visual Basic 2017) un literal binario. Si el literal entero está fuera del intervalo de un (es decir, si es menor o mayor que ), se produce un Byte Byte.MinValue error de Byte.MaxValue compilación.
En el ejemplo siguiente, los enteros iguales a 201 que se representan como literales decimales, hexadecimales y binarios se convierten implícitamente de Entero a byte valores.
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
Nota
Use el prefijo o para denotar un literal hexadecimal, el prefijo o para denotar un literal binario, y el prefijo o para &h &H denotar un literal &b &B &o &O octal. Los literales decimales no tienen prefijo.
A partir Visual Basic 2017, también puede usar el carácter de subrayado, , como separador de dígitos para mejorar la legibilidad, como se muestra en el _ ejemplo siguiente.
Dim byteValue3 As Byte = &B1100_1001
Console.WriteLine(byteValue3)
' The example displays the following output:
' 201
A partir Visual Basic 15.5, también puede usar el carácter de subrayado ( ) como separador inicial entre el prefijo y los dígitos hexadecimal, binario o _ octal. Por ejemplo:
Dim number As Byte = &H_6A
Para usar el carácter de subrayado como separador inicial, debe agregar el elemento siguiente al archivo del proyecto de Visual Basic (*.vbproj):
<PropertyGroup>
<LangVersion>15.5</LangVersion>
</PropertyGroup>
Para obtener más información, vea Select the Visual Basic language version (Seleccionar Visual Basic versión de idioma).
sugerencias de programación
Números negativos. Dado
Byteque es un tipo sin signo, no puede representar un número negativo. Si usa el operador unario menos ( ) en una expresión que se evalúa como de tipo , Visual Basic convierte-la expresión en primerByteShortlugar.Conversiones de formato. Cuando Visual Basic o escribe archivos, o cuando llama a archivos DLL, métodos y propiedades, puede convertir automáticamente entre formatos de datos. Los datos binarios
Bytealmacenados en variables y matrices se conservan durante estas conversiones de formato. No debe usar una variable para los datos binarios, ya que su contenido se puede dañar durante laStringconversión entre formatos ANSI y Unicode.Ampliación. El
Bytetipo de datos se amplía a , , , , , ,Short, oUShortIntegerUIntegerLongULongDecimalSingleDouble. Esto significa que puede convertirBytea cualquiera de estos tipos sin encontrar un System.OverflowException error.Caracteres de tipo.
Byteno tiene ningún carácter de tipo literal ni carácter de tipo identificador.Tipo de Framework. El tipo correspondiente en .NET Framework es la estructura System.Byte.
Ejemplo
En el ejemplo siguiente, b es una Byte variable. Las instrucciones muestran el intervalo de la variable y la aplicación de operadores de desplazamiento de bits en ella.
' 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)