Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to extract bytes from a long?

Reply
Thread Tools

How to extract bytes from a long?

 
 
Curt Geske
Guest
Posts: n/a
 
      10-15-2003
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
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      10-15-2003
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
 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-15-2003
Curt Geske <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
Serve La
Guest
Posts: n/a
 
      10-15-2003
"Irrwahn Grausewitz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >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.


 
Reply With Quote
 
David Rubin
Guest
Posts: n/a
 
      10-15-2003
Serve La wrote:
>
> "Irrwahn Grausewitz" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > >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
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-15-2003
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

 
Reply With Quote
 
cody
Guest
Posts: n/a
 
      10-15-2003
> >#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


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-15-2003
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?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
cody
Guest
Posts: n/a
 
      10-15-2003
> > > >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


 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      10-15-2003
In article <bmkgv9$npurr$(E-Mail Removed)-berlin.de>,
"cody" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
Private Bytes vs. # Bytes in all Heaps in Perfmon Jason Collins ASP .Net 3 02-18-2004 03:59 PM
Re: receiving Bytes and sending Bytes Ieuan Adams Computer Support 0 07-24-2003 07:46 PM
Re: receiving Bytes and sending Bytes The Old Sourdough Computer Support 0 07-23-2003 01:23 PM



Advertisments