"Join" elements of a single vector

2

I have a vector with four elements that are numbers represented in hexadecimal form.

What I need to do is concatenate these four elements (I think this is not the right word but I have not found a better one)?

For example:

int v[4]={0xA, 0xBB, 0x4B, 0x18};

I need a result that looks similar to:

int resultado=0xABB4B18;
    
asked by anonymous 24.02.2015 / 22:15

2 answers

2

It has a form to print only and one that actually calculates correctly and prints:

#include <stdio.h>

int main(void) {
    int v[4] = {0x0A, 0xBB, 0x4B, 0x18};
    for (int i = 0; i < 4; i++) {
        printf("%02X", v[i]);
    }
    return 0;
}

See running on ideone .

#include <stdio.h>

int main(void) {
    int v[4] = {0x0A, 0xBB, 0x4B, 0x18};
    int resultado = 0;
    for (int i = 0; i < 4; i++) {
        resultado *= 256;
        resultado += v[i];
    }
    printf("%08X", resultado);
    return 0;
}

See working on ideone .

    
24.02.2015 / 22:43
3

Well, if you multiply a HEXA number by 100 in HEXA, you "move" to the right.

For example:

  

in DECIMAL - > 13 * 100 = 1300
  in HEXADEC - > 0x13 * 0x100 = 0x1300

Try applying this concept to "concatenate" the HEXA values.

Another example:

  

If A = 0xE3, B = 0x4C, then A * 0x100 + B = 0xE34C.

    
24.02.2015 / 22:23