Byte Parsing (C Windows)

I have a DWORD value, which in Hex might look like this:

DWORD Val = 0xFF01091A

How can I read every byte? I mean, how can I read FF, 01, 09 and 1A?

Thanks!

+3
source share
6 answers
DWORD x1 = (0xFF01091A & 0xFF000000) >> 24;
DWORD x2 = (0xFF01091A & 0x00FF0000) >> 16;
DWORD x3 = (0xFF01091A & 0x0000FF00) >> 8;
DWORD x4 = (0xFF01091A & 0x000000FF) >> 0;
+2
source

This is actually no more:

uint8_t Byte1 = (Val>> 0) & 0xFF;
uint8_t Byte2 = (Val>> 8) & 0xFF;
uint8_t Byte3 = (Val>>16) & 0xFF;
uint8_t Byte4 = (Val>>24) & 0xFF;
+8
source
DWORD value;
// casts to `char *` are always ok:
unsigned char * bytes = (unsigned char *)&value;
unsigned char third_byte = bytes[2];

, : bytes[0] , .

, shifting, Efraim.

+5

:

union Bytes {
    char c[4];
    unsigned int n;
};

int main() {
    Bytes b;
    b.n = 0xFF01091A;
    char c = b.c[2];
}

: , ++. , C , ?

+2

Powered by AraK. Another way you can do this:

char x1 = *(((char*)&Val) + 0);
char x2 = *(((char*)&Val) + 1);
char x3 = *(((char*)&Val) + 2);
char x4 = *(((char*)&Val) + 3);

Make sure that this method is sensitive to the content of the machine.

+1
source

Mask and shift (as suggested in the previous answer); A slightly more complex alternative is the "pointer type arrow", i.e.

xlowbyte = (unsigned char*)(&Val) [0]; /* LSB, 0x1A */
xmidlowb = (unsigned char*)(&Val) [1]; /* 2nd-LSB, 0x09 */

etc. - provided that you are on a machine with small names (which will most likely be used for Windows).

+1
source

Source: https://habr.com/ru/post/1713489/


All Articles