Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C program to Extract each byte into a character variable

Status
Not open for further replies.

Parth86

Member
Hello
what is meaning of Extract each byte into a character variable (integer = hexadecimal value). does it means extract bytes from an integer (hexadecimal) value I don't understand logic behind to implement this program ?
 
Well, if you have 4-byte integer, you can extract the individual bytes and cast them to char-variable. There are many ways to do it.

C:
int variable = 0x12345678; /* 4 byte integer containing some value */
char byte0, byte1, byte2, byte3; /* Variables to hold the char-values */

/* extract the bytes as characters */
byte0 = (char)((variable >> (0*8)) & 0x000000FF);
byte1 = (char)((variable >> (1*8)) & 0x000000FF);
byte2 = (char)((variable >> (2*8)) & 0x000000FF);
byte3 = (char)((variable >> (3*8)) & 0x000000FF);

/* Another way using pointers */
byte0 = ((char*)(&variable))[0];
byte1 = ((char*)(&variable))[1];
byte2 = ((char*)(&variable))[2];
byte3 = ((char*)(&variable))[3];
 
Last edited:
Well, if you have 4-byte integer, you can extract the individual bytes and cast them to char-variable. There are many ways to do it.

C:
int variable = 0x12345678; /* 4 byte integer containing some value */
char byte0, byte1, byte2, byte3; /* Variables to hold the char-values */

/* extract the bytes as characters */
byte0 = (char)((variable >> (0*8)) & 0x000000FF);
byte1 = (char)((variable >> (1*8)) & 0x000000FF);
byte2 = (char)((variable >> (2*8)) & 0x000000FF);
byte3 = (char)((variable >> (3*8)) & 0x000000FF);

/* Another way using pointers */
byte0 = ((char*)(&variable))[0];
byte1 = ((char*)(&variable))[1];
byte2 = ((char*)(&variable))[2];
byte3 = ((char*)(&variable))[3];
Thank you very much for your quick response I was reading this code. whats happening in program. why there is same value &0xFF in all statement
C:
#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;
}
 
0x000000FF is a bit mask which is used to set all the upper bytes to zero "just in case". '&' is the bit-operator "AND".

byte0 = (char)(0x12345678 & 0x000000FF); /* The intermediate result of this is... */
byte0 = (char)(0x00000078); /* And after casting... */
byte0 = 0x78;

I think it is good habit to use the bit mask, but not necessary.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top