I'm trying to update 16 bit code to 32 bit code and unpack a given unsigned int to 4 characters. It's working alright except in two spots. I just started with this concept so I'm not strong with it yet. Can anyone please explain why two of the characters don't show up.
#include <stdio.h>
// prototypes
void unpackCharacters32(char *aPtr, char *bPtr, char *cPtr, char *dPtr, unsigned int pack);
void displayBits(unsigned int value);
int main(void)
{
// display bits of packed
unsigned int packed = 1818978115; // initialize packed value
puts("The packed character representation is:");
displayBits(packed);
char a; // first character unpacked
char b;// second character unpacked
char c;
char d;
unpackCharacters32(&a, &b, &c,&d, packed);
printf("\nThe unpacked characters are \'%c\',\'%c\',\'%c\' and \'%c\'\n", a, b,c,d);
displayBits(a);
displayBits(b);
displayBits(c);
displayBits(d);
}
// unpack two characters from pack
void unpackCharacters32(char *aPtr, char *bPtr, char *cPtr, char *dPtr, unsigned int pack)
{
unsigned int mask1=4278190080; // mask for first character
unsigned int mask2=16711680; // mask for second character
unsigned int mask3=65280;
unsigned int mask4=255;
*aPtr = (pack & mask1)>>8; // separate first character
*bPtr = (pack & mask2)>>8; // separate second character
*cPtr = (pack & mask3)>>8;
*dPtr = (pack & mask4);
}
// display the bits of value
void displayBits(unsigned int value)
{
unsigned int displayMask = 1 << 31; // bit mask
printf("%7u = ", value);
// loop through bits
for (unsigned int c = 1; c <= 32; c ) {
value & displayMask ? putchar('1') : putchar('0');
value <<= 1; // shift value 1 bit to the left
if (c % 8 == 0) { // print a space
putchar(' ');
}
}
putchar('\n');
}
here's what its supposed to look like
Pack: 1818978115➔ output should be 'l', 'k', 'g', and 'C'
here's what I'm getting
The unpacked characters are '','','g' and 'C'
0 = 00000000 00000000 00000000 00000000
0 = 00000000 00000000 00000000 00000000
103 = 00000000 00000000 00000000 01100111
67 = 00000000 00000000 00000000 01000011
CodePudding user response:
You're not shifting correctly. Since mask1
matches the high-order byte, you have to shift 3 bytes down, not just 1 byte. So it should be
*aPtr = (pack & mask1)>>24;
*bPtr = (pack & mask2)>>16;
*cPtr = (pack & mask3)>>8;
*dPtr = (pack & mask4);
CodePudding user response:
Use memcpy
int main(void)
{
// display bits of packed
unsigned int packed = 1818978115; // initialize packed value
puts("The packed character representation is:");
displayBits(packed);
char a[4]; // first character unpacked
unpackCharacters32(a, packed);
printf("\nThe unpacked characters are \'%c\',\'%c\',\'%c\' and \'%c\'\n", a[0], a[1], a[2], a[3]);
displayBits(a[0]);
displayBits(a[1]);
displayBits(a[2]);
displayBits(a[3]);
}
// unpack two characters from pack
void unpackCharacters32(char *aPtr, unsigned int pack)
{
memcpy(aPtr, &pack, sizeof(pack));
}
If you want to use bitshifts use HEX numbers not decimal as they digits represent exact numbers of bits (4)
BTW you do need to use any masks
void unpackCharacters32(char *aPtr, char *bPtr, char *cPtr, char *dPtr, unsigned int pack)
{
*dPtr = pack & 0xff; // separate first character
*cPtr = (pack >> 8) & 0xff;
*bPtr = (pack >> 16) & 0xff;
*aPtr = (pack >> 24) & 0xff;
}