I'm making a program in C. I am to extract bytes. un8 extractbyte (int r, int pos)
should return byte number pos from number r. As example, I use as input: 0x7788AABB
. Output then should be:
Part number 0 is BB
Part number 1 is AA
Part number 2 is 88
Part number 3 is 77
I am stuck at the last part of the program, where I have put the question marks in the comments. Those lines aren't right and I am confused in how I should make it work... The output I get now is bb at every part. I am pretty new at C by the way.
#include <stdio.h>
#include <stdlib.h>
typedef unsigned int un32;
typedef unsigned char un8;
un8 extractbyte (un32 r, un8 pos);
int main ()
{
un32 number;
un8 k;
printf("Enter a number:\n");
scanf("%x",&number);
for (k=0; k<=3;k++)
printf ("Part number %d is: %x \n", k , extractbyte(number, k) );
return 0;
}
un8 extractbyte (un32 r , un8 pos)
{
un32 mask;
un32 size = pos*8;
un32; // ??
un8; // ??
return (un8) r; // ??
}
I would make your
extractbyte
function something like this.