void loop() {
String str = "F03A42";
int start = 0;
int arr[2][19];
int t = 0;
//El 'for' lo hace 6 veces hasta que acaba la cadena de datos
for(int ends = 2;ends <= sizeof(str); ends += 2){
char s[2];
//Extra los 2 caracteres necesarios para la conversion
str.substring(start, ends).toCharArray(s, sizeof(s) + 1);
//Luego lo pasa a un array para luego procesarlo de otra manera
//Aqui el problema, si hago:
//arr[t] = (int)s & 0xFF; da un numero estatico (242)
arr[t] = s;
start += 2;
t++;
}
}
Example: F0 is written in hex (supposedly) but I need to pass that 2 char to a full-fledged byte.
void simplificado(){
//El byte
char bit[2] = 'F0';
//Ejemplo
byte d = CharToByte(bit);
//Pero que si convierto 'd' se igual a 240 (Que es el numero entero de 0xF0)
}
You can use
sscanf
, but for something this simple it may be convenient (efficiency) to program it yourself. For example (using plain C strings)leonbloy 's answer is correct but does not have to be valid for all platforms since not all character tables have all contiguous letters and numbers (like IBM's infamous EBCDIC ); Despite this, the code in your answer would work in many cases (even with many versions of EBCDIC), but to be on the safe side, I would like to propose an approach based on
switch
:This
swtich
works for any encoding, whether the letters and numbers are contiguous or not, for the conversion this simple function would suffice:And it can be used like this:
The code can be seen working [here] and the output is as follows: