problem of little indian to big indian code

problem of little indian to big indian code

Post by bruno.pati » Wed, 12 Jul 2000 04:00:00



I have an application that creates data on another platform than Linux
and when we read these datas on the linux box, we have to work on the
order of the bytes by eight block count. Is there a mean to do the work
automatically by options on the opening of the file ? where can i find
answers on this subject ?

thank you

 
 
 

problem of little indian to big indian code

Post by phil-news-nos.. » Wed, 12 Jul 2000 04:00:00



| I have an application that creates data on another platform than Linux
| and when we read these datas on the linux box, we have to work on the
| order of the bytes by eight block count. Is there a mean to do the work
| automatically by options on the opening of the file ? where can i find
| answers on this subject ?

If the application on the Linux box is one you can change the code for
(such as you wrote it yourself) then simply read the data in the byte
order you know it to be.

I don't know what you mean by "the order of the bytes by eight block count".
But if you know when in the input stream you have one of these integers
and how many bytes it is, then just read it one byte at a time and shift
it into an integer variable in the appropriate order.  If you are reading
a number in big endian, then you start by having a variable assigned with
the value 0, and for each byte read in, shift it left by 8 bits and add
in the new one.  If the number is little endian, you pre-shift the byte
by N times 8 bits where N is the byte count sequence starting with 0,
and add that to the accumulation variable.  The resultant code will be
a little slower, but will be portable (e.g. it will read the data in the
fixed byte order regardless of the byte order of the host running it).

There are also some tools around to do this that better optimize cases
where the re-ordering isn't needed.  You could use the network byte
order functions in the socket library if your data fits that scheme.

--
| Phil Howard - KA9WGN | My current websites: linuxhomepage.com, ham.org
| phil  (at)  ipal.net +----------------------------------------------------


 
 
 

problem of little indian to big indian code

Post by Sian Leit » Fri, 14 Jul 2000 04:00:00


Aig Tue, 11 Jul 2000 23:41:42 +0200,

Quote:>I have an application that creates data on another platform than Linux
>and when we read these datas on the linux box, we have to work on the
>order of the bytes by eight block count. Is there a mean to do the work
>automatically by options on the opening of the file ? where can i find
>answers on this subject ?

>thank you

(1) It's not "indian", but "endian", the "end" part referring to
those people who open their boiled eggs at a particular end (yes,
really!).

(2) What does "order of the bytes by eight block count" mean?

(3) Generally speaking, reversing endian-ness has to be done by
individual programs. While it is true that X servers cater for
different endianness of the client computer, other programs seem not
to do so. So you are probably on you own in this. However, here is a
procedure to reverse endianness of integers:-

PROC reverse endianness = (INT n)INT:
BEGIN
   INT
      bits per byte = 8,
      bytes per int = 4;
   BITS bn=BIN n;  #Convert to a mode where bits are accessible#
   BITS result:=2r0; #Set to zero#
   FOR i TO bytes per int
   DO
      BITS isolate byte=bn SHR bits per byte*(i-1) AND 16rff;
      result:=result OR isolate byte SHL bits per byte*(4-i)
   OD;
   ABS result
END; #reverse endianness#

A compiler for the above routine may be had merely by downloading the
file mentioned in my sig.
--
Sian Leitch (Software Engineer specialising in Algol 68)
Algol 68 for Linux is available from me or at
<ftp://vestein.arb-phys.uni-dortmund.de/pub>
Look for ctrans_0.1.0.tar.gz

 
 
 

problem of little indian to big indian code

Post by Grant Edwar » Sat, 15 Jul 2000 04:00:00




>>I have an application that creates data on another platform than Linux
>>and when we read these datas on the linux box, we have to work on the
>>order of the bytes by eight block count. Is there a mean to do the work
>>automatically by options on the opening of the file ? where can i find
>>answers on this subject ?

>(1) It's not "indian", but "endian", the "end" part referring to
>those people who open their boiled eggs at a particular end (yes,
>really!).

>(2) What does "order of the bytes by eight block count" mean?

>(3) Generally speaking, reversing endian-ness has to be done by
>individual programs. While it is true that X servers cater for
>different endianness of the client computer, other programs seem not
>to do so. So you are probably on you own in this. However, here is a
>procedure to reverse endianness of integers:-

If I were you, and I wanted to do portable binary stuff
(there's an oxymoron), I'd always write things in "network
order".  There are already macros available to convert between
host and network byte order: ntohs(), ntohl(), htons(),
htonl().  These macros get used a _lot_ in network stacks and
are probably as optimal as you can get.  Unfortunately, they're
only available for "short int" and "long int".  Network geeks
don't care about floats and doubles (or long long ints,
apparently).

Way back when, Mark Williams Co. used to sell a v7 clone called
Coherent which ran on various processors (8086, 68K, etc.).
They had a patent on storing data on disk in a standard byte
order.  So, if you write data to disk in network order you owe
them royalties -- perhaps it's expired.  I don't know that they
ever tried to enforce it, and MWC is, I believe, out of
business anyway.

--
Grant Edwards                   grante             Yow!  YOW!!! I am having
                                  at               fun!!!
                               visi.com            

 
 
 

problem of little indian to big indian code

Post by elle.. » Sat, 15 Jul 2000 04:00:00



> If I were you, and I wanted to do portable binary stuff
> (there's an oxymoron), I'd always write things in "network
> order".  There are already macros available to convert between
> host and network byte order: ntohs(), ntohl(), htons(),
> htonl().  These macros get used a _lot_ in network stacks and
> are probably as optimal as you can get.  Unfortunately, they're
> only available for "short int" and "long int".  Network geeks
> don't care about floats and doubles (or long long ints,
> apparently).

You _could_ use something like xdr (man 3 xdr, then run screaming for
a good book on rpc ;). However, be prepared for some inflation. I
believe xdr requires four bytes per unit.

--

 
 
 

1. Little Indian/Big Indian

Hi,

I have a file on Linux(little Indian m/c) and written
some long integer data in file. Now I get this file on solaris
(Big indian ) m/c (via binary ftp).
How can I get same data which I have entered on linux on
solaris m/c?

Basically I want how to convert little indian m/c file
to big indian m/c file and vice a versa.

Thanks,
Sandeep

2. Patch?: linux-2.5.6-pre1/drivers/scsi/pci2000.c compilation fixes + pci device ID table

3. big indian-little indian

4. crazy newbie' s experiements

5. Big/Little Indian

6. udp socket in a kernel module?

7. determine computer type (big or little indian)

8. ATI Graphics Pro Turbo dot clocks

9. Big Endian/Little Indian ?

10. GNU/Linux in the Indian business press..

11. REVIEW: An Indian newsletter on 'software freedom'

12. Indian ?

13. The Windows powered Indian.