linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* canonical byte swap macros?
@ 2005-07-11  1:02 Robert P. J. Day
  2005-07-11  2:48 ` Jeff Woods
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Robert P. J. Day @ 2005-07-11  1:02 UTC (permalink / raw)
  To: C programming list


  is there a standard set of byte swap macros/functions for doing
big/little endian conversion?  i have no interest in re-inventing the
wheel and i'm sure there's a universally-recognized set of macros for
this, no?

rday

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: canonical byte swap macros?
  2005-07-11  1:02 canonical byte swap macros? Robert P. J. Day
@ 2005-07-11  2:48 ` Jeff Woods
  2005-07-11 19:23 ` Glynn Clements
  2005-07-11 19:44 ` Steve Graegert
  2 siblings, 0 replies; 4+ messages in thread
From: Jeff Woods @ 2005-07-11  2:48 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: C programming list

Howdy.

At 7/10/2005 21:02 -0400, Robert P. J. Day wrote:
>is there a standard set of byte swap macros/functions for doing 
>big/little endian conversion?  i have no interest in re-inventing 
>the wheel and i'm sure there's a universally-recognized set of 
>macros for this, no?

The most common case where a programmer cares about byte-order is 
networking code.  Thus "man 3 byteorder" says:

>>htonl, htons, ntohl, ntohs - convert values between host and 
>>network byte order

according to http://tinyurl.com/9c8hu which redirects to
http://linux.com.hk/PenguinWeb/manpage.jsp?name=byteorder&section=3

--
Jeff Woods <kazrak+kernel@cesmail.net> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: canonical byte swap macros?
  2005-07-11  1:02 canonical byte swap macros? Robert P. J. Day
  2005-07-11  2:48 ` Jeff Woods
@ 2005-07-11 19:23 ` Glynn Clements
  2005-07-11 19:44 ` Steve Graegert
  2 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2005-07-11 19:23 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: C programming list


Robert P. J. Day wrote:

>   is there a standard set of byte swap macros/functions for doing
> big/little endian conversion?  i have no interest in re-inventing the
> wheel and i'm sure there's a universally-recognized set of macros for
> this, no?

glibc has bswap_{16,32,64} in byteswap.h, but I don't think that they
are specified by any standard.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: canonical byte swap macros?
  2005-07-11  1:02 canonical byte swap macros? Robert P. J. Day
  2005-07-11  2:48 ` Jeff Woods
  2005-07-11 19:23 ` Glynn Clements
@ 2005-07-11 19:44 ` Steve Graegert
  2 siblings, 0 replies; 4+ messages in thread
From: Steve Graegert @ 2005-07-11 19:44 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: C programming list

On 7/11/05, Robert P. J. Day <rpjday@mindspring.com> wrote:
> 
>   is there a standard set of byte swap macros/functions for doing
> big/little endian conversion?  i have no interest in re-inventing the
> wheel and i'm sure there's a universally-recognized set of macros for
> this, no?
> 

For quick and dirty solutions I sometimes use one of the following
(not favoured over any other solution, it just work for me):

#define swap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)

#define swap_32(x) \
     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))

uint64_t swap_64(uint64_t x) {
    union { 
        uint64_t ll;
        uint32_t l[2]; 
    } w, r;
    w.ll = x;
    r.l[0] = bswap_32 (w.l[1]);
    r.l[1] = bswap_32 (w.l[0]);
    return r.ll;
}


Kind Regards

    \Steve

--

Steve Graegert <graegerts@gmail.com> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-07-11 19:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-07-11  1:02 canonical byte swap macros? Robert P. J. Day
2005-07-11  2:48 ` Jeff Woods
2005-07-11 19:23 ` Glynn Clements
2005-07-11 19:44 ` Steve Graegert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).