473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

big endian vs little endian data

It appears that the data I/O methods for the RandomAccessFile class
(e.g. readInt() and writeInt()) handle data in "big endian format"
(most significant byte first). If you have a file where the data was
written in "little endian format" (least significant byte first" how
is the best way to read that data in Java?

Dave Eland
da******@oru.edu
Jul 17 '05 #1
2 21373
Dave Eland <da******@oru.edu> wrote in message news:<rh********************************@4ax.com>. ..
It appears that the data I/O methods for the RandomAccessFile class
(e.g. readInt() and writeInt()) handle data in "big endian format"
(most significant byte first). If you have a file where the data was
written in "little endian format" (least significant byte first" how
is the best way to read that data in Java?

Dave Eland
da******@oru.edu


OK, if your reading ints, pullout each int and reverse the bytes.

int i = input_stream.readInt();
i = ((i & 0x000000ff) << 24) + ((i & 0x0000ff00) << 8) +
((i & 0x00ff0000) >>> 8) + ((i & 0xff000000) >>> 24);
Hope this helps.

AJG
Jul 17 '05 #2
Dave Eland wrote:
It appears that the data I/O methods for the RandomAccessFile class
(e.g. readInt() and writeInt()) handle data in "big endian format"
(most significant byte first). If you have a file where the data was
written in "little endian format" (least significant byte first" how
is the best way to read that data in Java?

Dave Eland
da******@oru.edu


Look at ByteBuffer & company. Something like this:

RandomAccessFile file = new RandomAccessFile(filename, "r");
byte[] recordBuffer = new byte[RECORD_LENGTH];
ByteBuffer record = ByteBuffer.wrap(recordBuffer);
record.order(ByteOrder.BIG_ENDIAN);
FloatBuffer floatRecordBuffer = record.asFloatBuffer();
IntBuffer intRecordBuffer = record.asIntBuffer();

Use

file.seek(offset);
file.read(recordBuffer);

to read the file, and

intRecordBuffer.rewind();
intRecordBuffer.get(arrayOfInts, offset, sizeOfArray);

to get it into your arrayOfInts.

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joe C | last post by:
I have some code that performs bitwise operations on files. I'm trying to make the code portable on different endian systems. This is not work/school related...just trying to learn/understand. ...
3
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
3
by: ranjeet.gupta | last post by:
Dear All !! I am not sure the question which I am asking is correct or wrong, but I have heard that storing the data into the big endian helps in gting the more transfer rate, Means we can...
4
by: senthilrag | last post by:
Provided a unsigned integer say (32bits b31-b0 bits) and its required to get (b19-b10) . One approach is have a mask and right shift it #define BIT_MASK 0x000FFC00 uint32 variable; ...
14
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding...
2
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
5
by: mohamed.alam78 | last post by:
Greetings, How does one find the format of a binary file to be in Little Endian or big Endian before we start reading the file. Thanks
33
by: raghu | last post by:
Is it possible to know whether a system is little endian or big endian by writing a C program? If so, can anyone please give me the idea to approach... Thanks a ton. Regards, Raghu
8
by: ma740988 | last post by:
Data stored on a storage device is byte swapped. The data is big endian and my PC is little. At issue: There's a composite type ( a header ) at the front of the files that I'm trying to read in....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.