Set uint8_t to int

Asked

Viewed 237 times

0

I am receiving data and in my project storing in an array of uint8_t and then processing in variable int, it’s okay that I could leave everything int but this does not apply to the project.

int main()
{
    uint8_t data=0x13;
    int range=(int) data;

    printf("8 bits: %x\n", data); // Mostra 13
    printf("16 bits: %d\n", range); // Mostra 19

    return 0;
}

How can I assign 13 (uint8_t) to an int variable without it doing the conversion?

1 answer

0

What you did was, the first printf you printed as hexadecimal and the second you printed as decimal.

13 in hexadecimal is equal to 19 in decimal.

There was no truncation of the variable value.

uint8_t(1 byte) fits fully into a int(4 bytes)

Browser other questions tagged

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