Big-Endian Order? 
Author Message
 Big-Endian Order?

How do I convert Word, Integer and/or LongInt to "Big-Endian Order"?

--

Tore Aursand



Wed, 18 Jun 1902 08:00:00 GMT  
 Big-Endian Order?


Quote:
>How do I convert Word, Integer and/or LongInt to "Big-Endian Order"?

To convert integer and word use the standard function Swap(). There is
nothing built-in to convert longints, so you'll have to roll your own.

For longints, you might like the following inline procedure:

Function SwapLong(i: Longint): Longint;
{ Convert endian between big<-->little -/- little<-->big }
INLINE(
     $5A       { pop DX       pop words in reverse order }
    /$58       { pop AX                                  }
    /$86 /$E0  { xchg AH,AL   then swap byte pairs       }
    /$86 /$F2  { xchg DH,DL                              }
);

    ...red



Wed, 18 Jun 1902 08:00:00 GMT  
 Big-Endian Order?

Quote:

> How do I convert Word, Integer and/or LongInt to "Big-Endian Order"?

> --

> Tore Aursand

I may be able to help you if I knew what "Big-Endian Order" is.  I am
unfamiliar with this data type.  Please post a clearer message.

                                        Grendal

--
"Fingers on harpstrings, hero-swords, the acts, the
eyes of queens.  By that I kill you." - Beowulf



Wed, 18 Jun 1902 08:00:00 GMT  
 Big-Endian Order?

Quote:


> > How do I convert Word, Integer and/or LongInt to "Big-Endian Order"?

> > --

> > Tore Aursand

> I may be able to help you if I knew what "Big-Endian Order" is.  I am
> unfamiliar with this data type.  Please post a clearer message.

It's not a data type, it's a storage form for data.  "Endianness" refers
to how multiple-byte data is stored in memory.  The x86 family uses
"little endian" order, which means, the low order byte is stored, and is
followed immediately by the high order byte.  In big-endian, the high
order byte is stored first, then the low order byte.

For integer or word, you should just be able to swap the bytes:

function endianswap (w : word) : word; assembler;

asm
  mov   ax,[w]
  xchg  ah,al
end;

As for longint, I've heard a couple different answers on that, and since
I'm not sure which is correct, I won't try to offer a solution there.

Quote:
>                                         Grendal

> --
> "Fingers on harpstrings, hero-swords, the acts, the
> eyes of queens.  By that I kill you." - Beowulf

--
Scott Earnest                      | _,-""-_,-""-_,-""-_,-""-_,-""-_,-"
|

|

|


Wed, 18 Jun 1902 08:00:00 GMT  
 Big-Endian Order?

Quote:


> > How do I convert Word, Integer and/or LongInt to "Big-Endian Order"?

> I may be able to help you if I knew what "Big-Endian Order" is.  I am
> unfamiliar with this data type.  Please post a clearer message.

Big-Endian Order is how bytes from Words, Integers, Longints are stored.  
On IBM compatible PC's (x86/8088), 6502 processors words are stored:
 hex     decimal
00 01  = 256

The bigger is stored towards the end, hence the name "Big-Endian"

where as on Macs (I think), Unix etc...  256 would be stored as :
 hex     decimal
01 00  = 256

The biggest (Most-Significant Byte) is stored at the beginning.  However
the person asked  How to convert Word, Integer, longint to Big-Endian
format.  Turbo Pascal stores Words/Ints/Longints in Big-Endian format
already.  A simple way to separate the the two bytes in pascal from a
word/integer would be to use:

MostSig:=Hi( TheWord );
LeastSig:=Lo( TheWord);

--
/----------------------------------------------------------------------------\

| PGP Info: 2048/A8A1DCD5 : E0 9E 9B EF C8 E4 68 3D  B5 9C 72 4C EC 61 DD 7A |
\----------------------------------------------------------------------------/



Wed, 18 Jun 1902 08:00:00 GMT  
 Big-Endian Order?

Quote:



> > > How do I convert Word, Integer and/or LongInt to "Big-Endian Order"?

> > I may be able to help you if I knew what "Big-Endian Order" is.  I am
> > unfamiliar with this data type.  Please post a clearer message.

> Big-Endian Order is how bytes from Words, Integers, Longints are stored.
> On IBM compatible PC's (x86/8088), 6502 processors words are stored:
>  hex     decimal
> 00 01  = 256

> The bigger is stored towards the end, hence the name "Big-Endian"

Wrong.  You have the storage order correct, but the naming wrong.  The
ever indispensible Hacker's Dictionary has this to say:

:little-endian: adj. Describes a computer architecture in which,
   within a given 16- or 32-bit word, bytes at lower addresses have
   lower significance (the word is stored `little-end-first').  The
   PDP-11 and VAX families of computers and Intel microprocessors and
   a lot of communications and networking hardware are little-endian.
   See {big-endian}, {middle-endian}, {NUXI problem}.  The
   term is sometimes used to describe the ordering of units other than
   bytes; most often, bits within a byte.

Endianness refers to what comes *first*.  

Quote:
> where as on Macs (I think), Unix etc...  256 would be stored as :
>  hex     decimal
> 01 00  = 256

Yes, this is true, because the Motorola 68xxx family are *big*-endian
architecture processors.  But you can NOT say that of Unix, which is an
operating system, not a hardware implementation -- the system
architecture is what ultimately determines endianness, not the
software.  Unix on a RISC-based system would be big-endian, since that's
the way many RISC-systems order data.  But if you install Linux or some
other Unix distrubtion on a 486 system, it's going to rely on the x86
little endianness.

Quote:
> The biggest (Most-Significant Byte) is stored at the beginning.  However
> the person asked  How to convert Word, Integer, longint to Big-Endian
> format.  Turbo Pascal stores Words/Ints/Longints in Big-Endian format
> already.  A simple way to separate the the two bytes in pascal from a
> word/integer would be to use:

Again, no, it does not.  The x86 family is little-endian.

Quote:
> MostSig:=Hi( TheWord );
> LeastSig:=Lo( TheWord);

That doesn't accomplish anything when you're reading big-endian data
into a little-endian system.  It's stil bass ackwards.  Suggestions
posted elsewhere in the thread.

Quote:
> --
> /----------------------------------------------------------------------------\

> | PGP Info: 2048/A8A1DCD5 : E0 9E 9B EF C8 E4 68 3D  B5 9C 72 4C EC 61 DD 7A |
> \----------------------------------------------------------------------------/

--
Scott Earnest                      | _,-""-_,-""-_,-""-_,-""-_,-""-_,-"
|

|

|


Wed, 18 Jun 1902 08:00:00 GMT  
 Big-Endian Order?

Quote:


>> How do I convert Word, Integer and/or LongInt to "Big-Endian Order"?

>> --

>> Tore Aursand

>I may be able to help you if I knew what "Big-Endian Order" is.  I am
>unfamiliar with this data type.  Please post a clearer message.

>                                    Grendal

   The Big Endian Order is the opposite of the Little Endian Order :=)

   Ok: an Intel processor store the low part of a number first and after,
the high part.  So, in memory, the 65500 number is stored as DC FF and
not FF DC  (the low part is stored before the high one).

   In order to convert to Big Endian (why?????), simply isolate the two
part with combinaison of a SHR and a AND and make a simple permutation.

   (Hope this is right but I think yes)

Quote:

>--
>"Fingers on harpstrings, hero-swords, the acts, the
>eyes of queens.  By that I kill you." - Beowulf

--

           ___                                              ___
           L_|_                                            _|_J
          ( -O>                                            <O- )
       ___//\J  __________________________________________  L/\\___
      //-,\    |                                          |    /,-\\
     || / \\   L   AVONTURE Christophe (c) AVC Software   J___// \ ||

  / \ //\\.    |__________________________________________|    .//\\ / \
 |_/\'/  ||                                                    ||  \'/\_|
      '   ||_       "Postings are personnal, and don't        _||   '
          |__)         reflect Belgacom's opinion"           (__|

               SWAG: http://www.gdsoft.com/swag/swag.html              

                        ftp://ftp.gdsoft.com/swag/



Wed, 18 Jun 1902 08:00:00 GMT  
 Big-Endian Order?



Quote:
>How do I convert Word, Integer and/or LongInt to "Big-Endian Order"?

For completeness, I cite the original reference :
        in Gulliver's Travels, by Jonathan Swift, 1726 :
        A Voyage to Lilliput, near the end of Chapter 4.

--

    http://www.merlyn.demon.co.uk/



Wed, 18 Jun 1902 08:00:00 GMT  
 Big-Endian Order?

Quote:


> > The bigger is stored towards the end, hence the name "Big-Endian"

> Wrong.  You have the storage order correct, but the naming wrong.  The
> ever indispensible Hacker's Dictionary has this to say:
> Endianness refers to what comes *first*.  

Sorry for any confusion I may have added, I just "assumed" and probably
shouldn't have.   I've been dealing with Big-Endian format all the time
and never knew it was even called that.

Quote:
> > where as on Macs (I think), Unix etc...  256 would be stored as :
> >  hex     decimal
> > 01 00  = 256

> Yes, this is true, because the Motorola 68xxx family are *big*-endian
> architecture processors.  But you can NOT say that of Unix, which is an
> operating system, not a hardware implementation -- the system
> architecture is what ultimately determines endianness, not the
> software.  Unix on a RISC-based system would be big-endian, since that's

Meant RISC CPU'd Unix systems.

Quote:
> the way many RISC-systems order data.  But if you install Linux or some
> other Unix distrubtion on a 486 system, it's going to rely on the x86
> little endianness.

> > The biggest (Most-Significant Byte) is stored at the beginning.  However
> > the person asked  How to convert Word, Integer, longint to Big-Endian
> > format.  Turbo Pascal stores Words/Ints/Longints in Big-Endian format
> > already.  A simple way to separate the the two bytes in pascal from a
> > word/integer would be to use:

> Again, no, it does not.  The x86 family is little-endian.

> > MostSig:=Hi( TheWord );
> > LeastSig:=Lo( TheWord);

> That doesn't accomplish anything when you're reading big-endian data
> into a little-endian system.  It's stil bass ackwards.  Suggestions
> posted elsewhere in the thread.

It still gives you the MostSig, and LeastSig of TheWord, but in
little-endian.    BTW, wouldn't it be quicker than Shifting to just use
assembler to swap the byte-order around?


Wed, 18 Jun 1902 08:00:00 GMT  
 Big-Endian Order?

: >
: > How do I convert Word, Integer and/or LongInt to "Big-Endian Order"?
: >
: > --

: > Tore Aursand

I just read this whole and didn't see the answer I would have given, so
here goes:
a) as far as words go, TP6 has a function called Swap which makes it
incredibly trivial to convert a 2-byte format between little<->big endian.
   Value := Swap(Value);   {swaps the hi and lo bytes}
You'll have to do more work to convert a LongInt cos it doesn't change the
middle bytes.
b) Somebody asked why you'd want to do it.  The simple answer is that some
platform-independent files (eg MIDI files) are stored as little-endian.
Of course there might be other reasons you'd want to as well.

Hope that helps!
Graham



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. big big tip for speeding things up

2. big big tip for speeding things up

3. QReport : BIG BIG Problem with memo-fields !!!

4. Order By does not order By!!!!

5. 'Lockfile grown too big' error

6. How BIG can Delphi App Get?

7. The Biggest BUG!

8. Some questions about big tables in Delphi 1.0

9. Big IN clause cause access violation

10. BLOB-field bigger then 32K in a Sybase SQL Anywhere database

11. Big Trouble of Application of designed by Delphi3.0 Client/Server on NT4.0+SP4 above

12. Extract BIG EXE File from SQLServer 6.5

 

 
Powered by phpBB® Forum Software