![]() |
How to extract bytes from a long?
I'm suprised no one suggested a union!
#include <stdio.h> union _x { long lng; char byt[4]; } X; void main( void ) { int i; // Always need an 'i', ... long val = 0x12345678; X.lng = val; for( i = 3; i >= 0; i-- ) { printf( "%#02x\n", X.byt[i] ); } } // main |
Re: How to extract bytes from a long?
Curt Geske wrote:
> I'm suprised no one suggested a union! > > #include <stdio.h> > union _x > { > long lng; > char byt[4]; > } X; > > void main( void ) > { > int i; // Always need an 'i', ... > long val = 0x12345678; > > X.lng = val; > for( i = 3; i >= 0; i-- ) > { > printf( "%#02x\n", X.byt[i] ); > } > > > } // main We wanted solutions that didn't exhibit undefined behaviour, ta very much. Or even unspecified behaviour - like, is X.byt[0] 0x12 or 0x78 or 0x34 or 0x56, and why? -- Chris "electric hedgehog" Dollin C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html |
Re: How to extract bytes from a long?
Curt Geske <cgeskeNOSPAM@compuserve.com> wrote:
If you want to reply to a post, please use the "Reply" function of your newsreader and do not start a new thread. Thank you. >I'm suprised no one suggested a union! > >#include <stdio.h> > union _x > { > long lng; > char byt[4]; Stop right here: what makes you think a long is four chars wide? <SNIP> Regards -- Irrwahn (irrwahn33@freenet.de) |
Re: How to extract bytes from a long?
"Irrwahn Grausewitz" <irrwahn33@freenet.de> wrote in message
news:bkrqovouo474goa4q8148tand1r084gn1m@4ax.com... > >I'm suprised no one suggested a union! > > > >#include <stdio.h> > > union _x > > { > > long lng; > > char byt[4]; > > Stop right here: what makes you think a long is four chars wide? That could be easily overcome with sizeof. It's more that you can't rely on the byte order if you try to do it portably. |
Re: How to extract bytes from a long?
Serve La wrote:
> > "Irrwahn Grausewitz" <irrwahn33@freenet.de> wrote in message > news:bkrqovouo474goa4q8148tand1r084gn1m@4ax.com... > > >I'm suprised no one suggested a union! > > > > > >#include <stdio.h> > > > union _x > > > { > > > long lng; > > > char byt[4]; > > > > Stop right here: what makes you think a long is four chars wide? > > That could be easily overcome with sizeof. It's more that you can't rely on > the byte order if you try to do it portably. No, the problem is that you can't write into one union field and read from another. /david -- Andre, a simple peasant, had only one thing on his mind as he crept along the East wall: 'Andre, creep... Andre, creep... Andre, creep.' -- unknown |
Re: How to extract bytes from a long?
Curt Geske wrote:
> I'm suprised no one suggested a union! Since your suggestion involves non-portable implementation-defined behavior (in addition to the obvious illiteracy for the return type for main), I'm surprised that anyone would suggest it. > > #include <stdio.h> > union _x > { > long lng; > char byt[4]; > } X; > > void main( void ) I'm surprised you have the gall to write the above line. Please wake up. Your nap is now at least 14 years long. > { > int i; // Always need an 'i', ... > long val = 0x12345678; > > X.lng = val; > for( i = 3; i >= 0; i-- ) > { > printf( "%#02x\n", X.byt[i] ); > } Try the following on for size. Not that "the most recent store" was to the '.lng' member, which is different from the '.byt' member. [#5] With one exception, if the value of a member of a union object is used when the most recent store to the object was to a different member, the behavior is implementation-defined.70) One special guarantee is made in order to simplify the use of unions: If a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members. > > > } // main -- Martin Ambuhl |
Re: How to extract bytes from a long?
> >#include <stdio.h>
> > union _x > > { > > long lng; > > char byt[4]; > > Stop right here: what makes you think a long is four chars wide? #include <stdio.h> union _x { long lng; char byt[sizeof long]; } better? -- cody [Freeware, Games and Humor] www.deutronium.de.vu || www.deutronium.tk |
Re: How to extract bytes from a long?
cody wrote:
> > > >#include <stdio.h> > > > union _x > > > { > > > long lng; > > > char byt[4]; > > > > Stop right here: what makes you think a long is four chars wide? > > #include <stdio.h> > union _x > { > long lng; > char byt[sizeof long]; > } > > better? Marginally. It still doesn't address the issue raised by both David Rubin and Martin Ambuhl, and it still uses an identifier reserved for the implementation, and it still risks trouble with trap representations or if the system uses signed magnitude or ones' complement arithmetic ... ... but other than that, Mrs. Lincoln, how did you like the play? -- Eric.Sosman@sun.com |
Re: How to extract bytes from a long?
> > > >I'm suprised no one suggested a union!
> > > > > > > >#include <stdio.h> > > > > union _x > > > > { > > > > long lng; > > > > char byt[4]; > > > > > > Stop right here: what makes you think a long is four chars wide? > > > > That could be easily overcome with sizeof. It's more that you can't rely on > > the byte order if you try to do it portably. > > No, the problem is that you can't write into one union field and read > from another. Isn't that what a union is good for??? -- cody [Freeware, Games and Humor] www.deutronium.de.vu || www.deutronium.tk |
Re: How to extract bytes from a long?
In article <bmkgv9$npurr$1@ID-176797.news.uni-berlin.de>,
"cody" <dont.spam.me.deutronium@gmx.de> wrote: > > > > >I'm suprised no one suggested a union! > > > > > > > > > >#include <stdio.h> > > > > > union _x > > > > > { > > > > > long lng; > > > > > char byt[4]; > > > > > > > > Stop right here: what makes you think a long is four chars wide? > > > > > > That could be easily overcome with sizeof. It's more that you can't rely > on > > > the byte order if you try to do it portably. > > > > No, the problem is that you can't write into one union field and read > > from another. > > > Isn't that what a union is good for??? Stay with freeware. Don't even think about getting a paid job as a programmer. |
| All times are GMT. The time now is 12:41 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.