Diferencia entre revisiones de «byte»

De ArduWiki
Saltar a: navegación, buscar
(Vea también)
(Ejemplo 1)
Línea 52: Línea 52:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Ejemplo 1 ==
+
== Ejemplo 2 ==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
byte x, y, z = 12;        //Error, x e y están declaradas pero no inicializadas.
 
byte x, y, z = 12;        //Error, x e y están declaradas pero no inicializadas.
 
byte x = 12,y = 12,z = 12; //Ok
 
byte x = 12,y = 12,z = 12; //Ok
 +
</syntaxhighlight>
 +
 +
== Ejemplo 3 ==
 +
En este ejemplo hacemos la variable byte igual a un '''caracter'''.
 +
 +
<syntaxhighlight lang="c++">
 +
void setup() {
 +
  Serial.begin(9600);
 +
  Serial.println("Letras mayusculas:");
 +
  for (char c='A'; c<='Z'; c++){
 +
      Serial.write(c);            //Imprime el carácter
 +
      Serial.print(", dec: ");
 +
      Serial.println(c);          //Imprime su valor decimal
 +
  }
 +
}
 +
void loop() {
 +
  //Nada
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revisión del 13:16 24 feb 2019

Descripción

Un byte almacena un número sin signo de un byte (8 bits), de 0 a 255 (2^8 - 1). Alternativamente puede verse declarado como tipo uint8_t o unsigned char, sin embargo funciona exactamente igual.

Nota: Es importante tener en mente que un numero del tipo byte puede contener hasta 8 valores 0/1 (que es sinonimo de verdadero/falso) empacados en un solo byte y que se puede operar con ellos con operadores bit a bit y funciones internas de bits y byte.


Sintaxis

byte variable [= valor]; 
uint8_t variable [= valor]; 
unsigned char variable [= valor]; 

Parametros

variable
nombre de la variable.
valor
valor entre 0~255. Parámetro opcional.
Posibles formateadores
Base Prefijo Comentario Ejemplo
DEC ninguno Dígitos 0~9 123
HEX 0x dígitos 0~9 + Caracteres A~F 0x7B
OCT 0 digitos 0~7 0173
BIN B 0 o 1 B1111011

Comentario

unsigned char es lo mismo que byte y se prefiere este ultimo.

Advertencias

  • Si sumas 1 al valor máximo que de 255 pasa a 0.
  • Si restas 1 al valor mínimo que de 0 pasa a 255.
byte a = 255;
a++;   //a es 0 
byte b = 0;
b--;   //b es 255

Ejemplos

byte n = 'A';
byte n = 65;         //Valor ASCII de letra A
uint8_t n = 65;
byte x = 0x41;       //0x = formato hexadecimal, 65
byte x = 0101;       //0 = formato octal, 65
byte x = B1000001;   //B = formato binario, 65

Ejemplo 2

byte x, y, z = 12;         //Error, x e y están declaradas pero no inicializadas.
byte x = 12,y = 12,z = 12; //Ok

Ejemplo 3

En este ejemplo hacemos la variable byte igual a un caracter.

void setup() {
   Serial.begin(9600);
   Serial.println("Letras mayusculas:");
   for (char c='A'; c<='Z'; c++){
      Serial.write(c);            //Imprime el carácter
      Serial.print(", dec: ");
      Serial.println(c);          //Imprime su valor decimal
   }
}
void loop() {
   //Nada
}

Vea también

Referencias