Convert char array to one byte

2
    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 hexadecimal (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)
}
    
asked by Hector Seguro 17.06.2016 в 04:53
source

2 answers

1

You can use sscanf , but for something so simple you can agree (efficiency) to program it yourself. For example (using plain C strings)

#include<stdio.h>
// convierte un caracter hexa al valor entero. 
int parseHexaChar(char A) {
    return (A > '9')? (A &~ 0x20) - 'A' + 10: (A - '0');
}

int main(void)
{
      char str[] = "F03A42";
      int bytes =  sizeof(str)/2; // cuidado: habría que asegurarse que el string tenga largo par
      char *p = &str[0];
      int i;
      for( i=0; i< bytes;i++) {
            int val = parseHexaChar(*p++)*16 + parseHexaChar(*p++);
            printf("%d\n",val);
      }
}
    
answered by 17.06.2016 / 15:46
source
0

The response of leonbloy is correct but does not have to be valid for all platforms because not all the character tables have all the letters and contiguous numbers (such as the infamous IBM EBCDIC ); despite this the code of your answer would work in many cases (even with many versions of EBCDIC), but to heal in health I would like to propose an approximation based on switch :

int char_to_int(char c)
{
    switch (c)
    {
        case '0': return 0;
        case '1': return 1;
        case '2': return 2;
        case '3': return 3;
        case '4': return 4;
        case '5': return 5;
        case '6': return 6;
        case '7': return 7;
        case '8': return 8;
        case '9': return 9;
        case 'a': case 'A': return 0xA;
        case 'b': case 'B': return 0xB;
        case 'c': case 'C': return 0xC;
        case 'd': case 'D': return 0xD;
        case 'e': case 'E': return 0xE;
        case 'f': case 'F': return 0xF;
        default:
            printf("Error '%c' no valido", c);
            exit(-1);
    }

    return 0;
}

This swtich works for any coding, whether the letters and numbers are contiguous or not, for conversion this simple function would suffice:

int hex_to_int(char *hex, int length)
{
    int result = 0;

    for (char *c = hex, *e = c + length; c != e; ++c, --length)
    {
        result |= (char_to_int(*c) << ((length - 1) * 4));
    }

    return result;
}

And it can be used like this:

int main()
{
    char fabada[] = "fabada";
    char cafe[]   = "cafe";
    char baboso[] = "bab050";

    printf("fabada: %d\n", 0xfabada);
    printf("fabada: %d\n", hex_to_int(fabada, 6));

    printf("cafe: %d\n", 0xcafe);
    printf("cafe: %d\n", hex_to_int(cafe, 4));

    printf("bab050: %d\n", 0xbab050);
    printf("bab050: %d\n", hex_to_int(baboso, 6));

    return 0;
}

The code can be seen working [here] and the result is as follows:

fabada: 16431834
fabada: 16431834
cafe: 51966
cafe: 51966
bab050: 12234832
bab050: 12234832
    
answered by 20.06.2016 в 10:57