high endian code for intel

Programming, for all ages and all languages.
Post Reply
Ozguxxx

high endian code for intel

Post by Ozguxxx »

Hi, I need some ideas about how to mutate little endian values into high endian values, god d*** jpeg files use high endian byte order and does anybody have some trick to solve this problem otherwise Ill have to call a function for every value that I read from file. I know that it is not that hard to write that function but I need a fast function, some bit shifts would do my job. Illustration:

Ozguxxx: Im in a terrible trouble and I need help!
HQ: Hold on xxx, _programming_gurus are on the way to get you out...
Ozguxxx: I hope they wont be late, I have four days to get out of this hell.
HQ: I said hold on!!!
Ozguxxx: AAAAArggghhhh!!! dzzzzzzzzzzzzzzzzzz... {noise}
HQ: All _programming_gurus contact lost to Ozguxxx, help him, NOW!
ark

Re:high endian code for intel

Post by ark »

I don't think there is any bit-shifting trick (at least I'm not aware of one), because you're going to be reversing the byte order:

A
B
C
D

becomes

D
C
B
A

Just like there's no memmove trick (that I'm aware of)you can use to convert the string "ABCD" into the string "DCBA".

If it's practical, it might not be a bad idea to just use an already-written function like ntohl or ntohs, which should be reasonably efficient.
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:high endian code for intel

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:17 pm, edited 1 time in total.
Jamethiel

Re:high endian code for intel

Post by Jamethiel »

If you're using C/C++, look for the macros htons(), htonl(), ntohs(), and ntohl(). They are probably in <netinet/in.h> on a *nix system, and possibly in the winsock headers on a *ows system.

These macros do conversion from "network byte order" (also known as 'big endian') to "native byte order", (also known as 'the right way round') and back. They will typically be coded as inline ASM for speed.

Hope this helps.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:high endian code for intel

Post by Pype.Clicker »

at a lower level, doesn't the "XLATB" instruction of the CPU perform just the right transformation ?
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:high endian code for intel

Post by df »

nah xlat is an index translation.

eg: if AL had 0x43, BX=0x100 its like doing mov al,b[bx + al] type thing...

probably why nody uses xlat, its a useless instruction :)
-- Stu --
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:high endian code for intel

Post by Pype.Clicker »

oops . sorry. I always mix BSWAP (which should do what requested) and XLATB :-/
Post Reply