Thread: Using the pack function

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    Using the pack function

    Hi all,

    I have written a c program that prompts the user for integers 1 and 2 and then prints out that number in different way. Here it is

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    void main () {
    char c;
    int i, n1, n2, number;
    long k;
    float f;
    while () {
    printf ("enter first number: ");
    scanf("d%", &n1);
    printf ("d%", &n1);
    printf ("enter last number: ");
    scanf("d%", &n2);
    printf ("d%", &n2);
    for (number=n1; number <=n2; number++) {
    c = number;
    i = number;
    k = number;
    f = number;
    printf("char: (dec) = \t%12d (uns dec) = %12u (hex) = %12x\n",c,c,c);
    printf("int: (dec) = \t%12d (uns dec) = %12u (hex) = %12x\n",i,i,i);
    printf("char: (dec) = \t%12ld (uns dec) = %12lu (hex) = %12lx\n",k,k,k);
    printf("f: (float) = \t %12.3f\n\n\n", f);
    }
    }
    }
    My problem is that from that program I must write a function pack (a,b) to pack two 8-bi "char" variables a and b into a 16 bit "int" variable. I am supposed to modify the program that i wrote above to call the pack function as instructd by the user. The values a and b are entered in hex and teh packed variables are printed as decimal numbers.

    pack (0, 80) = 128
    pack (0,ff) = 255

    I am not sure how i can do this nor do i know how to use the pack function. Can someone help me please? Help me please!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What happens if input variables a and b are 32 bit ints? Are they discarded or are they cast and packed?
    If a=0x10 and b=0x80 are packed together into a 16 bit space, what is the value printed out then?

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    no you can only input two 8 bit values for a and b.

    for instance:

    pack (ff,01) = -255

    pack (7f,ff) = 32767

    pack (80,00) = -32768

    Pack (a,b) and the inputs a and be will be 2 hex for a and 2 for b. So the input can only be a 16 bit input never a 32 bit in my case.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, to give a big hint: If I gave you the numbers 4 and 7, how could you combine them to get 47 using arithmetic?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    using arithmetic?
    Does multiplication count?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Does multiplication count?
    It is one of the big four of Ambition, Distraction, Uglification, and Derision.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    How 'bout this? I'm not sure if I understood the point but I think I did:
    Code:
    #include <stdio.h>
    #include <linux/types.h>
    
    typedef __s16 dword;
    
    struct input {
    	char A;
    	char B;
    };
    
    union twobytes {
    	struct input this;
    	dword that;
    };
    
    dword pack (char a, char b) {
    	union twobytes example;
    	example.this.A=a;
    	example.this.B=b;
    	return example.that;
    }	
    
    int main() {
    	char a=0, b=1;
    	printf("%d\n",pack(a,b));
            return 0;
    }
    answer: 256 (which makes sense to me). I think the arguments are in the reverse order from the OP.

    ps. Is there a more portable source of specific bitsized datatypes than this? I just dug it out of "usr/include" because I didn't want to rely on short.
    Last edited by MK27; 02-20-2009 at 11:33 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    How 'bout this? I'm not sure if I understood the point but I think I did:
    Code:
    #include <stdio.h>
    #include <linux/types.h>
    
    typedef __s16 dword;
    
    struct input {
    	char A;
    	char B;
    };
    
    union twobytes {
    	struct input this;
    	dword that;
    };
    
    dword pack (char a, char b) {
    	union twobytes example;
    	example.this.A=a;
    	example.this.B=b;
    	return example.that;
    }	
    
    int main() {
    	char a=0, b=1;
    	printf("%d\n",pack(a,b));
            return 0;
    }
    answer: 256 (which makes sense to me). I think the arguments are in the reverse order from the OP.

    ps. Is there a more portable source of specific bitsized datatypes than this? I just dug it out of "usr/include" because I didn't want to rely on short.
    That's certainly a method, but as you noticed, endianness can play havoc with what you want to do (hence the suggestion of multiplication).

    If you're willing to put up with C99, you have all the types in stdint.h.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Bitshifting is multiplication, right?
    Code:
    int pack (unsigned char a, unsigned char b) {
    	int A=a<<8;
    	return A+b;
    }
    The arguments are the correct way around now.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MK27 View Post
    Bitshifting is multiplication, right?
    Code:
    int pack (unsigned char a, unsigned char b) {
    	int A=a<<8;
    	return A+b;
    }
    The arguments are the correct way around now.
    correct except for the endianness

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    correct except for the endianness
    I was going by this from the OP:
    pack (0, 80) = 128
    pack (0,ff) = 255
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    correct except for the endianness
    If you mean "correct period", then I would agree with you. There is no endianness involved in bitshifting, and so there can be no endianness problems in the above. (Remember "<<" doesn't mean "shift to a lower address", it means "shift to more significant bits".)

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    My ooops!, I noticed that right after I posted it; the OS routine will load the low byte into the low(high) address depending on the endiannes.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MK27 View Post
    I was going by this from the OP:
    pack (0, 80) = 128
    pack (0,ff) = 255
    you're right.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    Does multiplication count?
    We're setting a really low bar for what "arithmetic" means, aren't we?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

Tags for this Thread