C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-19-2009, 06:53 PM   #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!
tbrown81 is offline   Reply With Quote
Old 02-19-2009, 07:22 PM   #2
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
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?
itCbitC is offline   Reply With Quote
Old 02-20-2009, 02:12 AM   #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.
tbrown81 is offline   Reply With Quote
Old 02-20-2009, 10:53 AM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?
tabstop is offline   Reply With Quote
Old 02-20-2009, 10:56 AM   #5
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by tabstop View Post
using arithmetic?
Does multiplication count?
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 02-20-2009, 11:01 AM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by MK27 View Post
Does multiplication count?
It is one of the big four of Ambition, Distraction, Uglification, and Derision.
tabstop is offline   Reply With Quote
Old 02-20-2009, 11:31 AM   #7
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 02-20-2009 at 11:33 AM.
MK27 is offline   Reply With Quote
Old 02-20-2009, 11:35 AM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 02-20-2009, 11:52 AM   #9
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 02-20-2009, 12:00 PM   #10
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
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
itCbitC is offline   Reply With Quote
Old 02-20-2009, 12:03 PM   #11
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 02-20-2009, 12:07 PM   #12
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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".)
tabstop is offline   Reply With Quote
Old 02-20-2009, 12:13 PM   #13
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
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.
itCbitC is offline   Reply With Quote
Old 02-20-2009, 12:14 PM   #14
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
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.
itCbitC is offline   Reply With Quote
Old 02-20-2009, 12:48 PM   #15
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by MK27 View Post
Does multiplication count?
We're setting a really low bar for what "arithmetic" means, aren't we?
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Reply

Tags
c programming, pack function

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling sample DarkGDK Program Phyxashun Game Programming 6 01-27-2009 03:07 AM
Seg Fault in Compare Function tytelizgal C Programming 1 10-25-2008 03:06 PM
Another syntax error caldeira C Programming 31 09-05-2008 01:01 AM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM


All times are GMT -6. The time now is 02:57 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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