Hey everyone....Im trying to learn about unions and Im finding it hard too grasp with the reference I have...Im trying to write this program that uses a union to combine a long int and a struct with four members...I first have to ask a user to enter a hex value in and the program is to display the user value in both hex and decimal...then using the structure members, display the user value one byte at a time in both hex and decimal..my question is that if I read in a long int(4bytes I hope) but it only takes in the first 4 hex values(2 bytes) and ignores everything before? I have very limited knowledge and have been playing/trying to understand union manipulations...and I could really use a boost to get me to where (and hopefully how) to read individual bytes(I can read the LSB in hex but not long int(%ld(right))...thanks for lookin...
Code:* * Program Description: This program asks the user to input an integer * value in Hex and displays it in hex and decimal * form.Then each byte of the structure is displayed * in decimal form. * * */ #include <stdlib.h> #include <ctype.h> #include <string.h> #include <stdio.h> #include <conio.h> struct bytes { unsigned char byte1; unsigned char byte2; unsigned char byte3; unsigned char byte4; }; union master { long value; struct bytes complete; }num; void intro(void); char ragain(void); void signoff(void); void pause(void); int main(void) { char input = 'y'; clrscr(); intro(); while(input == 'Y' || input == 'y') { fflush(stdin); printf("please enter a hexvalue..(ie e34f2)"); scanf("%xH",&num.value); printf("the value is:%ld\n",num.value); printf("the lsb is %xH dec is ?\n",num.complete.byte1); input=ragain(); } signoff(); pause(); return 0; } /* End of Main */



LinkBack URL
About LinkBacks


