473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Packing data...

Hello all, quick hopefully easy question. Here goes.

I've started on the Art Of Assembly tutorial(s)
(http://webster.cs.ucr.edu/Page_asm/0_ArtOfAsm.html)
and got to the sample projects and kinda got stuck. I'm needing to use a
language of my choosing (which
i easily chose C) to do this project where it wants me to pack data into a
16 bit variable. Now i am
familiar with C to a pretty decent extent, just not bit manipulation. So
what i'm needing help on is getting
this data packed into an unsigned short int, data type.

The example it gives is : 0100 0001 0101 1000 or 4158h
0100 being a 4 for the month,
00010 being 2 for the day, and
1011000 being 88 for the year.

These will come into my function as 3 seperate variables.I'm using 2 single
characters for the month & day, then an array for the year.(they need to
enter
in say 1988 for the above example, and i just grab out the 88. Not a
problem).
So i would basically get something along the lines of, myFunc('4', '2',
"1988");
And i just need help getting those values in their correct "spot" packed
into my
16 bit data type. (I can get 1 of them, but after the first one i keep
messing
up the way it's packed in by trying to pack the next value in.). As i said
i'm
still learning this stuff, so i'm kinda picking up on things as i go. Any
help,
and or a comments are definatly welcome. I've been searching google and such
for
help but so far have found none. Thought i'd give here a shot. Thx in
advance all,
....Frank M...

Nov 13 '05 #1
2 5929
On Mon, 25 Aug 2003 13:02:56 GMT, "FMorales" <al****@comcast.net>
wrote:
Hello all, quick hopefully easy question. Here goes.

I've started on the Art Of Assembly tutorial(s)
(http://webster.cs.ucr.edu/Page_asm/0_ArtOfAsm.html)
and got to the sample projects and kinda got stuck. I'm needing to use a
language of my choosing (which
i easily chose C) to do this project where it wants me to pack data into a
16 bit variable. Now i am
familiar with C to a pretty decent extent, just not bit manipulation. So
what i'm needing help on is getting
this data packed into an unsigned short int, data type.

The example it gives is : 0100 0001 0101 1000 or 4158h
0100 being a 4 for the month,
00010 being 2 for the day, and
1011000 being 88 for the year.
Bitfield packing is done with the left-shift operator and the
bitwise inclusive or operator:

unsigned short month = 4;
unsigned short day = 2;
unsigned short year = 88;
unsigned short dmy;

dmy = month << 12 | day << 7 | year;

However it's a good idea to add range checks and/or masking so
that one field cannot overflow into the adjacent field.
These will come into my function as 3 seperate variables.I'm using 2 single
characters for the month & day, then an array for the year.(they need to
enter
in say 1988 for the above example, and i just grab out the 88. Not a
problem).


I'm glad it's "not a problem". You can combine your solution to
this bit with the packing operation from above.

Nick.

Nov 13 '05 #2
On Mon, 25 Aug 2003 13:02:56 GMT
"FMorales" <al****@comcast.net> wrote:
So what i'm needing help on is getting this data packed into an unsigned short
int, data type.
bitfields or shifts.
The example it gives is : 0100 0001 0101 1000 or 4158h
0100 being a 4 for the month,
00010 being 2 for the day, and
1011000 being 88 for the year.

So i would basically get something along the lines of, myFunc('4', '2',
"1988");
what about december 31 2003? myFunc ('12', '31', "03")?
And i just need help getting those values in their correct "spot" packed
into my
16 bit data type. (I can get 1 of them, but after the first one i keep
messing
up the way it's packed in by trying to pack the next value in.).


You can pack it into a data type, but it's printed value will change when you
run it on a big-endian system.

union {
unsigned short int si;
struct {
unsigned year:7;
unsigned day:5;
unsigned month:4;
} bf;
} cram;
main() {
cram.bf.month = 4;
cram.bf.day = 2;
cram.bf.year = 88;
printf ("%X\n", cram.si);
}

or if you like less readable but much shorter code:

main () {
unsigned short int sint = 0;
sint = 4<<12 | 2<<7 | 88;
printf ("%X\n", sint);
}

I'd pick the first option. It's understandable, and reading the variables is
easier.

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Victor S. Miller | last post by:
I'm sure that this has been asked and answered before, but about 1/2 hour of searching hasn't turned up anything. I have external data that requires the packing and unpacking of long vectors of...
0
by: William Goedicke | last post by:
Dear Y'all - I'm need to "unpack" fields in a data file that was created with COBOL on an OpenVMS machine. I've tried Convert::IBM390 but it appears that the packing algorithm is different. ...
0
by: nobull | last post by:
Google won't let me post this as a follow-up but lynch@agere.com (lynto) wrote in message news:<503469eb.0407271148.7ee4b0ca@posting.google.com>... > Does anyone have an algorithm for packing...
2
by: gbb0330 | last post by:
Hi all I need some advice. the daily packing number will be used to match a box with its label. the guys in the warehouse will get a list of items to pack. they will find the item - put it...
1
by: gbb0330 | last post by:
hi guys -the code (all capital letters) generates daily packing number, it is in the on click event of a command button that moves to the next record. dpkn is unbound control - i use it to...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
1
by: Gajendra | last post by:
How does the byte packing and the byte alignment work in VC++ compiler? What is the effect of #pragma pack(n) on the alignment and byte packing for example while using the structur struc double...
2
by: Sonnich | last post by:
Hi all! I am still playing around with databases. So here it goes: Is packing needed for MySQL (5.0)? I have a new DB, which already is 178 MB. Will that continue to grow, when I add and...
9
by: fraz | last post by:
Does anyone have code to solve a 3-d bin packing problem in php? I am trying to solve the issue of packing various sized rectangular shaped objects into boxes of three different sizes so as to use...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.