Convert "unsigned int" to "unsigned char" vector?

Asked

Viewed 557 times

5

I need to convert a unsigned int in a vector of unsigned char to later translate this address into binary, for a work that needs to simulate a virtual memory. Can someone explain to me how to do this?

3 answers

1

The answer you chose as best can be responsible for Undefined behaviour this is may change chunks of memory that do not belong to you causing crashes or bugs. If I were you I would use a [Union][2] to convert, easier, faster and viable.

Take the example here:

#include <stdio.h>
#include <stdlib.h>

int main (void) {

   typedef union _Conversor
   {
       unsigned int inicial; //Valor que vai armazenar
       unsigned char leitor[4];//Para ler todos os bytes unsigned int tem 4 em windows e linux
    }Conversor;

    Conversor conv;
    conv.inicial = 0xCE87;//Valor random
    printf("%02X %02X %02X %02X\n", conv.leitor[0], conv.leitor[1], conv.leitor[2], conv.leitor[3]);


    return 0;   
}

http://ideone.com/58FMdE

  • I even like the idea of using Union badly, but I haven’t seen how the other solution can have an undefined behavior. You can explain this statement?

  • @prmottajr I have tested and compiled the other solution program works. I even printed the sizeof(buf) just to be sure and gave me 8. Being the size of an int normally 4, no for loop ends up exceeding the limit of the string. Imagine that in a consecutive position you have another string or an int or a structure. You will end up editing it. I hope I have been enlightening in my answer. By the way, what is the problem with Union? May be unusual but is very useful in this type of operation

  • Like I said, I like the Union approach. Perhaps the priblema with the other solution and that also affects Union is that the sizes of the data types vary with the machine architecture. I think it’s worth a sizeof(int) right?

  • @prmottajr You’re right but since I don’t think this program is intended to be ported to more OS you can use INT. Even so, the int size varies according to the architecture of the machine, so a good programmer should use the header inttypes.h that contains int8_t, int16_t and more. So its size does not vary.

  • But regardless of OS change (linux, windows or mac os) between 32 and 64 bit architecture also varies.

  • 1

    @prmottajr [See here] According to this ( https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models ) the size of the int does not vary from 32 bits to 64 bits for windows.

  • +1 by reference, it was a good debate. I’ve had many problems with long and pointers. I ended up assuming the same for int

Show 2 more comments

1


If I understand correctly, you want to reinterpret an integer vector for a "byte" vector. So, you can do this here:

#include <stdio.h>
#include <stdlib.h>

#define ASIZE(x) sizeof(x)/sizeof(x[0])

int main (void) {

    const unsigned int teste[5] = {300, 500, 900, 1100, 1300};
    unsigned char *buf = NULL;
    size_t jmp = 0;

    buf = malloc(ASIZE(teste));
    if (!buf) abort();

    for (size_t index = 0; index < ASIZE(teste); index++,jmp+=sizeof(int)) {
            buf[jmp] = teste[index] & 0x000000ff;
            buf[jmp+1] = (teste[index] & 0x0000ff00) >> 8;
            buf[jmp+2] = (teste[index] & 0x00ff0000) >> 16;
            buf[jmp+3] = (teste[index] & 0xff000000) >> 24;
            printf("%u - %u - %u - %u\n", buf[jmp+3], buf[jmp+2], buf[jmp+1], buf[jmp]);
    }

    return 0;
}

The above code makes use of bit manipulation. If you don’t know what this is, just take a look here: http://www.programiz.com/c-programming/bitwise-operators

0

Basically something like this

unsigned int valor = 1042;
unsigned char vetor[38]; // escolher limite apropriado
int len = snprintf(vetor, sizeof vetor, "%u", valor);
// vetor 'e a string "1042", ou seja
// vetor[0] == '1'; vetor[1] = '0'; vetor[2] = '4'; vetor[3] = '2'; vetor[4] = '\0';

If you want to convert to values instead of characters, decrease '0' every character

for (int k = 0; k < len; k++) vetor[k] -= '0'; // vetor ja nao 'e uma string!
// vetor[0] == 1; vetor[1] = 0; vetor[2] = 4; vetor[3] = 2;
  • In this case I will only be printing the int unsigned as an unsigned char, I need to convert and save it into a variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.