Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How to extract bytes from a long? (http://www.velocityreviews.com/forums/t315783-how-to-extract-bytes-from-a-long.html)

Curt Geske 10-15-2003 02:59 PM

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

Chris Dollin 10-15-2003 03:12 PM

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

Irrwahn Grausewitz 10-15-2003 03:59 PM

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)

Serve La 10-15-2003 04:56 PM

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.



David Rubin 10-15-2003 06:22 PM

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

Martin Ambuhl 10-15-2003 07:25 PM

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


cody 10-15-2003 08:47 PM

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



Eric Sosman 10-15-2003 08:54 PM

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

cody 10-15-2003 10:16 PM

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



Christian Bau 10-15-2003 10:21 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57