Byte order

How does one find out the byte order of the platformon which JVM is running? For example, for a Java application that has to read a C structure from a file, the structure wouldbe stored in the local byte order and Java is big endian.

First a little background for new guys.

A memory module M can be viewed as an array of cells. Each cell hasa unique address and holds one byte. Let M[i] denote the byte storedin the cell at address i.

Consider the following declaration:

short x = 1;
Since x occupies two bytes,
 x = 00000000 00000001  // binary representation of 1
we will have to use two consecutive cells, say M[2000] and M[2001],to store x. But which byte goes in which cell?

Great battles have been fought over this seemingly trivial detail,recalling the battle between the Lilliputians and theirneighbors over which was the proper end to break on a soft boiledegg: the little end or the big end.

The Big Endians (Motorola, SPARC, Java VM) insist that the big (most significant) end ought to come first:

M[2000] = 00000000   M[2001] = 00000001
The Little Endians (Intel) insist that the little end ought tocome first:
M[2000] = 00000001   M[2001] = 00000000
(MIPS processors can be configured either way!)

Actually, the choice is not as trivial as it first seems. Writing a number to a binary file preserves the byte order, so a BigEndian machine reading a file of numbers written by a Little Endian machine must perform complicated conversions, and vice versa.

The Java VM always writes data to files in Big Endian order, and always assumes the data files it reads are Big Endian.

So how do we tell if the host platform is Big or Little Endian?(Unfortunately, byte order isn’t normally in System.getProperties().)I’m going to go out on a limb and say that there isn’t a way to dothis from within a Java program. If there was, it would breakJava’s platform independence. I tried doing a quasi-legal cast, the standard technique used in C, but Java was too smart for me:

class Parent {}   class Child1 extends Parent{ short x = 1; }   class Child2 extends Parent{ byte a, b; }      Child1 c1;   Parent p = c1;       // generalize   Child2 c2 (Child2)p; // specialize
Notice that c1 and c2 both occupy two bytes. My plan was to peek at the first byte of c1.x:
boolean bigEndian = (c2.a == 0);
Fortunately (or unfortunately) the Java VM busted me on the specialization of p to c2. This happened because Java does both staticand dynamic type checking. In other words, c1 had a typetag indicating that it was an instance of Child1, which theJava VM consulted before attempting the cast (Child2) p.

So how does one determine the byte order of the host platform?This would be of interest to people attempting to read binaryfiles generated by native programs from their Java programs. The only solution is to go outside, or partially outside, ofJava by writing 1 to a file from a C program:

int fd = creat(“foo”, 0777);   fprintf(fd, “%d”, 1);
Then read this file into a Java program:
 FileInputStream fin = new FileInputStream(“foo”);   DataInputStream din = new DataInputStream(fin);   int x = din.readInt();   boolean bigEndian = (x == 1);
Of course this won’t work in an applet because you can’tread from or write to local files; but then, an applet shouldn’tneed to know the byte order of its host.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.