[comp.os.msdos.programmer] Reversing Ints easily in TC++

jdries%doppler@faatcrl (J. Francis Dries, III) (10/26/90)

I was hacking up a program to convert Sun Rasterfiles to GIF files when
I ran into a little problem.  Almost all the non-Intel world (or so I'm
told) writes ints MSByte first, whereas Borland's TC++ is
reading/writing ints/longs (I need longs, since I'm working with 32bit
ints from the Sun) LSByte first.

My Question is, is there a way to reverse the order, quickly or cleanly?
I could read in each char at a time and OR it onto the long and SHL all
4 on, but that seems kinda sloppy to me.  I would think that the is a
flag that can be set, or at least a built in macro...

Any ideas?

---
J. Francis Dries, III
...!rutgers!faatcrl!doppler!jdries           jdries%doppler@faatcrl

"You can put a bullet in my head, but you can't kill a word I said"
                                                    -- Mike Muir

dmurdoch@watstat.waterloo.edu (Duncan Murdoch) (10/26/90)

In article <i6TmR3w163w@doppler> jdries%doppler@faatcrl (J. Francis Dries, III) writes:
>I was hacking up a program to convert Sun Rasterfiles to GIF files when
>I ran into a little problem.  Almost all the non-Intel world (or so I'm
>told) writes ints MSByte first, whereas Borland's TC++ is
>reading/writing ints/longs (I need longs, since I'm working with 32bit
>ints from the Sun) LSByte first.
>
>My Question is, is there a way to reverse the order, quickly or cleanly?
>I could read in each char at a time and OR it onto the long and SHL all
>4 on, but that seems kinda sloppy to me.  I would think that the is a
>flag that can be set, or at least a built in macro...

The i486 has a new instruction BSWAP that does exactly what you want in 1
clock (if the data is already in a register), but I suppose that doesn't 
help you much. I'd do it by loading the two word sized parts, exchanging the 
bytes of each, and writing them out again.  Something like:

   mov ax, word ptr [data]
   mov dx, word ptr [data+2]
   xchg al,ah
   xchg dl,dh
   mov word ptr[data],dx
   mov word ptr[data+2],ax

teittinen@cc.helsinki.fi (10/29/90)

In article <i6TmR3w163w@doppler>, jdries%doppler@faatcrl (J. Francis
Dries, III) writes:
> Almost all the non-Intel world (or so I'm
> told) writes ints MSByte first, whereas Borland's TC++ is
> reading/writing ints/longs (I need longs, since I'm working with 32bit
> ints from the Sun) LSByte first.
> 
> My Question is, is there a way to reverse the order, quickly or cleanly?
> 
> Any ideas?

If my memory serves me right (I don't have the manual with me right
now) there is a library routine named swab() which does the byte
swapping for you.

I think that it is very interesting to glimpse through the manuals once
in a while and find out some interesting and useful library functions... 

-- 
E-Mail: teittinen@finuh.bitnet               ! "Studying is the only way
        teittinen@cc.helsinki.fi             !  to do nothing without
Marko Teittinen, student of computer science !  anyone blaming you" -me

dfoster@jarthur.Claremont.EDU (Derek R. Foster) (10/31/90)

In article <i6TmR3w163w@doppler> jdries%doppler@faatcrl (J. Francis Dries, III) writes:
>I was hacking up a program to convert Sun Rasterfiles to GIF files when
>I ran into a little problem.  Almost all the non-Intel world (or so I'm
>told) writes ints MSByte first, whereas Borland's TC++ is
>reading/writing ints/longs (I need longs, since I'm working with 32bit
>ints from the Sun) LSByte first.
>
>My Question is, is there a way to reverse the order, quickly or cleanly?

In the case of ints, look up the "swab" function. I don't know about longs,
though.

Derek Riippa Foster