# Thread: Using the pack function

1. ## 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. 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. 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. 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. Originally Posted by tabstop
using arithmetic?
Does multiplication count?

6. Originally Posted by MK27
Does multiplication count?
It is one of the big four of Ambition, Distraction, Uglification, and Derision.

7. 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.

8. Originally Posted by MK27
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. 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.

10. Originally Posted by MK27
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. Originally Posted by itCbitC
correct except for the endianness
I was going by this from the OP:
pack (0, 80) = 128
pack (0,ff) = 255

12. Originally Posted by itCbitC
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. 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. Originally Posted by MK27
I was going by this from the OP:
pack (0, 80) = 128
pack (0,ff) = 255
you're right.

15. Originally Posted by MK27
Does multiplication count?
We're setting a really low bar for what "arithmetic" means, aren't we?