unions

This is a discussion on unions within the C Programming forums, part of the General Programming Boards category; Hey everyone....Im trying to learn about unions and Im finding it hard too grasp with the reference I have...Im trying ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    unions

    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		*/

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >read in a long int(4bytes I hope) but it only takes in the first 4 hex values(2 bytes) and ignores everything before?

    what decimal number are you entering?

    if its less than 32,000 its not gonna use the upper two bytes.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    thanks for looking ....It's intended that the user enters a hex value....I entered the same hex digits as the example e34f2 and it would only store the 4 LSB's and drop the 'e'. I tried other five digit hex values but the variable num.value would only hold the first 4LSB's??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, find out then:

    printf("%d", sizeof long );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Sorry to be nitpicky.
    The only portable C89 way to use printf to print the result of sizeof is to use ul and to cast the result of sizeof to unsigned long
    printf("sizeof(i)=%lu\n",(unsigned long) sizeof(i));
    If C99 we have %zd.
    The one who says it cannot be done should never interrupt the one who is doing it.

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >
    I entered the same hex digits as the example e34f2 and it would only store the 4 LSB's and drop the 'e'. I tried other five digit hex values but the variable num.value would only hold the first 4LSB's??
    <

    your only entering 3 bytes of data there are 2 chars per hex byte(ie. 0x4f is one byte, 0x12345678 is four bytes), with 5 characters your gonna have and one and a half empty bytes.

    print all the bytes like so, you should get all the characters you input.

    printf("byte 1 is 0x%02xh\n",num.complete.byte1);
    printf("byte 2 is 0x%02xh\n",num.complete.byte2);
    printf("byte 3 is 0x%02xh\n",num.complete.byte3);
    printf("byte 4 is 0x%02xh\n",num.complete.byte4);

    Sample output on an Intel x86(Little-Endian or LSB first) processor:

    please enter a hexvalue..(ie e34f2)e34f2
    the value is:931058
    byte 1 is 0xf2h
    byte 2 is 0x34h
    byte 3 is 0x0eh
    byte 4 is 0x00h

    does this help you at all?
    Last edited by no-one; 02-27-2003 at 11:03 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    thanks alot for all your help ....the problem was that I had to read the hex digits as a long hex which worked ...thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unions of the same size and pointers
    By holychicken in forum C Programming
    Replies: 9
    Last Post: 10-06-2008, 04:29 PM
  2. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  3. Structures - Unions
    By AProg in forum C Programming
    Replies: 16
    Last Post: 05-27-2003, 01:43 AM
  4. unions problems!
    By nextus in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2003, 04:04 AM
  5. Unions in c++
    By The Gweech in forum C++ Programming
    Replies: 5
    Last Post: 08-06-2002, 09:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21