ADVERTISEMENT
ADVERTISEMENT

C program to extract bytes from an integer (Hexadecimal) value

This program will extract bytes values from an integer (hexadecimal) value. Here we will take an integer value in hexadecimal format and then extract all 4 bytes in different four variables. The logic behind to implement this program - right shift value according to byte position and mask it for One byte value (0xff).

Extract bytes from an integer (Hexadecimal) value using C program

/*C program to extract bytes from an integer (Hex) value.*/

#include <stdio.h>

typedef unsigned char BYTE;

int main()
{
    unsigned int value = 0x11223344; //4 Bytes value

    BYTE a, b, c, d; //to store byte by byte value

    a = (value & 0xFF); //extract first byte
    b = ((value >> 8) & 0xFF); //extract second byte
    c = ((value >> 16) & 0xFF); //extract third byte
    d = ((value >> 24) & 0xFF); //extract fourth byte

    printf("a= %02X\n", a);
    printf("b= %02X\n", b);
    printf("c= %02X\n", c);
    printf("d= %02X\n", d);

    return 0;
}

Output:

    a= 44
    b= 33
    c= 22
    d= 11

C Advance Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.