🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #16203

Re: Byte swapping

#16203
From: Chris Hill
Date: Wed, 17 Mar 1999 00:00
30 lines
1721 bytes
On Wed, 17 Mar 1999 10:23:33 -0700, Doug Bath <dbath@agt.net> wrote:

>    In 4 byte words (long ints or floats) I need to swap byte 1 with byte 4 and
>byte 2 with byte 3. Thanks to your help I was able to get the following to work
>
> private int swap4(int s)
>    {
>        int tmp1, tmp2, tmp3, tmp4;
>        tmp1 = (s & 0xFF) << 24;
>        tmp4 = (s >> 24) & 0xFF;
>        tmp2 = (s  & 0x0000FF00) << 8;
>        tmp3 = (s >> 8)  & 0x0000FF00;
>        return tmp1 | tmp2 | tmp3 | tmp4;
>    }

Looks good.

>It started to make sense when I realized that &0xFF meant leave the bits the same.
>The above works for long ints and negatives such as -2048. I need to try it on
>more data and make a version for floats. I would appreciate any suggestions or
>warnings you may have about this function and thanks again for your help.

For floats you should probably read the number as an int, use your
existing integer byte swap routine (assuming the bytes need to be
swapped in the same way), then call Float.intBitsToFloat (if your
float is stored in the format that Java uses after the byte swap).


Chris Hill
Chris244@aol.com
Technical Details
Message-ID: <36eff869.148375332@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36EE8AA3.3212A98D@agt.net> <36eeb7cc.66297370@forums.borland.com> <36EEE3CA.F6F4D6A3@agt.net> <36eef8d9.82950857@forums.borland.com> <36EFE515.ECA654BB@agt.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,a228e969071f0feb
X-Google-Attributes: gidf74b3,public
From: Chris244@aol.com (Chris Hill)
Subject: Re: Byte swapping
Date: 1999/03/17
Message-ID: <36eff869.148375332@forums.borland.com>
X-Deja-AN: 455868512
References: <36EE8AA3.3212A98D@agt.net> <36eeb7cc.66297370@forums.borland.com> <36EEE3CA.F6F4D6A3@agt.net> <36eef8d9.82950857@forums.borland.com> <36EFE515.ECA654BB@agt.net>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.language