Big endian and little endian in java

Bit manipulation | Swap Endianness of a number

Prerequisite: https://www.geeksforgeeks.org/little-and-big-Endian-mystery/
Little Endian and Big Endian are ways or storing data in machines. Some machines might use Little Endian byte ordering while others might use big Endian. This creates an inconsistency when you are transferring data from a Big Endian machine to a Little Endian machine. Usually, the compiler takes care of the conversion. But, in networking, Big Endian is used as the standard for the exchange of data between networks. Therefore, Little Endian machines need to convert their data to Big Endian while sending data through a network. Similarly, Little Endian machines need to swap the byte ordering when they receive data from a network.
So Endianness comes into picture when you are sending and receiving data across the network from one host to another host. If the sender and receiver computer have different Endianness, then there is a need to swap the Endianness so that it is compatible.
Therefore, it is important to convert the data to little Endian or big Endian so that there is consistency and data integrity. In this article, we will look at how the Endianness of a number can be swapped. This is also a common interview question.

Approach :

  1. Get the rightmost 8 bits of the number by anding it with 0x000000FF since the last 8 bits are all ones and the rest are all zeros, the result will be rightmost 8 bits of the number. The result is stored in a variable called leftmost_byte
  2. Similarly, get the next 8 bits (from the right, right middle) of the number by anding it with 0x0000FF00. The result is stored in left_middle_byte
  3. Obtain the next 8 bits of the number by anding it with 0x00FF0000. The result is stored in right_middle_byte
  4. Finally, get the leftmost 8 bits of the number by anding it with 0xFF000000. The result is stored in rightmost_byte
  5. Now that we have all the 4 bytes of the number, we need to concatenate it in reverse order. i.e, swap the Endianness of the number. To do this, we shift the rightmost 8 bits by 24 to the left so that it becomes the leftmost 8 bits. We left shift the right middle byte by 16 (to store it as the left middle byte) We left shift the left middle byte by 8 (to store it as the right muddle byte) We finally left shift the leftmost byte by 24 to the left
  6. Now, we logically “or” (concatenate) all the variables to obtain the result.
Читайте также:  Html style text color red

Consider the number 0x12345678. The number is 4 bytes wide. In Big Endian, this number is represented as:

In Little Endian, the same number is represented as:

Input : 0x12345678
Output : 0x78563412
Input : 0x87654321
Output : 0x21436587

Implementation:

Источник

Little-Endian and Big-Endian in Java

We must have heard the terms Little-Endian and Big-Endian many times in your engineering course. Let’s quickly recap the concept behind these words.

1. Little-Endian vs Big-Endian

These two terms are related to the direction of bytes in a word within CPU architecture.

Computer memory is referenced by addresses that are positive integers. It is “natural” to store numbers with the least significant byte coming before the most significant byte in the computer memory.

Sometimes computer designers prefer to use a reversed-order version of this representation.

The “natural” order, where the less significant byte comes before the more significant byte in memory, is called little-endian.

Many vendors like IBM, CRAY, and Sun preferred the reverse order that, of course, is called big-endian.

For example, the 32-bit hex value 0x45679812 would be stored in memory as follows:

Address 00 01 02 03 ----------------------------------- Little-endian 12 98 67 45 Big-endian 45 67 98 12 

The difference in endian-ness can be a problem when transferring data between two machines.

Everything in Java binary files is stored in big-endian order. This is sometimes called network order. This means that if you use only Java, all files are done the same way on all platforms: Mac, PC, UNIX, etc. You can freely exchange binary data electronically without any concerns about endian-ness.

The problem comes when you must exchange data files with some program not written in Java that uses little-endian order, most commonly a program written in C. Some platforms use big-endian order internally (Mac, IBM 390); some uses little-endian order (Intel).

4. How to Know the Endian-ness

In Java, we can use the method ByteOrder.nativeOrder() to obtain the byte order used by the CPU. Below is the output on an Intel CPU.

ByteOrder byteOrder = ByteOrder.nativeOrder(); System.out.println(byteOrder); //LITTLE_ENDIAN

Java hides that internal endian-ness from us and gives us consistent results in all the platforms.

But in programming languages, where code reads the data directly from the memory areas using pointers, endian-ness could be an issue in cases where data is transferred from one machine to another machine and both machines have different endian-ness.

Источник

Java Byte Buffer and Byte Order, Big endian or Little endian

A byte order is the order in which bytes of a multi-byte value are stored.

Suppose you have a short value 300 stored in a variable as follows:

A short value is stored in two bytes.

The value 300 can be represented in 16-bits as 0000000100101100.

The right-most bit is the least significant bit, which is 00101100.

The left-most bit is the most significant bit, which is 00000001.

We can store them as follows:

0000000100101100 which is 00000001 followed by 00101100 0010110000000001 which is 00101100 followed by 00000001.

If we know the order of the bytes, you can compute the correct value 300 using either form of the 16 bits: 0000000100101100 or 0010110000000001.

Big endian

A byte order is called big endian if the bytes of a multi-byte value are stored from the most significant byte to the least significant byte.

Big endian means most significant first.

If you store a short value of 300 as 0000000100101100, you are using the big endian byte order.

Little endian

A byte order is called little endian if the bytes of a multi-byte value are stored from the least significant byte to the most significant byte.

Little endian means least significant first.

In the little endian byte order, you would store 300 as 0010110000000001, which seems backwards for representing a 16-bit value.

Example

Suppose you read two bytes as 0000000100101100.

If it is in a big endian byte order, it represents a value of 300.

If it is in a little endian byte order, it represents a value of 11265.

Java uses a big-endian byte order to store data.

By default, a byte buffer uses a big endian byte order.

ByteOrder

An instance of the java.nio.ByteOrder class represents a byte order.

This class has no public constructor.

You can use two constants, BIG_ENDIAN and LITTLE_ENDIAN, which are defined in the ByteOrder class to represent these byte orders.

A byte order is meaningful only in a multi-byte value stored in a byte buffer.

Example 2

The following code demonstrates how to get and set byte order for a byte buffer.

You use the order() method of the ByteBuffer class to get or set the byte order.

The program stores a short value of 300 in two bytes of a byte buffer.

It displays the values stored in the first and the second bytes using both big endian and little endian byte orders.

The output shows the values of bytes in decimal as 1 and 44, whose binary equivalents are 00000001 and 00101100, respectively.

The following code shows how to set the Byte Order of a Byte Buffer.

import java.nio.ByteBuffer; import java.nio.ByteOrder; public class Main < public static void main(String[] args) < ByteBuffer bb = ByteBuffer.allocate(2); System.out.println("Default Byte Order: " + bb.order()); bb.putShort((short) 300); bb.flip(); // w ww . d e m o 2s . co m showByteOrder(bb); // Repopulate the buffer in little endian byte order bb.clear(); bb.order(ByteOrder.LITTLE_ENDIAN); bb.putShort((short) 300); bb.flip(); showByteOrder(bb); > public static void showByteOrder(ByteBuffer bb) < System.out.println("Byte Order: " + bb.order()); while (bb.hasRemaining()) < System.out.print(bb.get() + " "); > System.out.println(); > >

  • Java Create Different Views of a Buffer
  • Java Char Set Encoder and Decoder
  • Java Get list of all available character sets supported by the JVM
  • Java Byte Buffer and Byte Order, Big endian or Little endian
  • Java Knowing the Byte Order of a Machine
  • Java NIO Channels
  • Java NIO Charset, Character Set

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

How do I convert a big-endian(64 bit) to little endian(64 bit) in Java

send pies

posted 15 years ago

  • Report post to moderator
  • How do I convert a big-endian(64 bit) to little endian(64 bit) in Java and vice versa.

    For e.g. I have 40429C28F5C28F5C in big endian, I want to convert it into little endian (5C8FC2F5289C4240) and vice versa.

    How would I code a function to do this in Java?

    Sheriff

    Chrome

    send pies

    posted 15 years ago

  • Report post to moderator
  • SCJP 1.4 — SCJP 6 — SCWCD 5 — OCEEJBD 6 — OCEJPAD 6
    How To Ask Questions How To Answer Questions

    author

    jQuery Chrome

    send pies

    posted 15 years ago

  • Report post to moderator
  • You don’t need third party tools for this — the java.nio.ByteBuffer class can do this for you.

    Just allocate a byte buffer of at least 8 bytes, set the order to big endian, put in the long value, set the order to little endian, reset the position to zero, and get the long value back from the buffer.

    send pies

    posted 15 years ago

  • Report post to moderator
  • I tried to read through and use the java.nio.ByteBuffer class , but I am little confused as I am new to Java.

    Can you please explain me with an example.

    send pies

    posted 15 years ago

  • Report post to moderator
  • I have tried to follow the steps you have mentioned :

    // Create an empty ByteBuffer with a 8 byte capacity
    ByteBuffer bbuf = ByteBuffer.allocate(8);

    //Set the order to Big Endian
    bbuf.order(ByteOrder.BIG_ENDIAN) ;

    // Use the putLong() for putting in the Long Value.
    bbuf.putLong((long) 37.22);

    //Set the order to Little Endian
    bbuf.order(ByteOrder.LITTLE_ENDIAN) ;

    // Get the Long value back
    ByteBuffer b1 = bbuf.getLong((long) 37.22 );

    Its giving some error in conversion from Long to Byte.

    Wanderer

    send pies

    posted 15 years ago

  • Report post to moderator
  • I think Henry was assuming (as was I) that you’d be using a long value. Instead it looks like you’re using a double. In that case, don’t use putLong() and getLong() — use putDouble() and getDouble(). Also, the call to getLong() or getDouble() shouldn’t have 37.22 passed in as an argument. You’ve already used that number; it’s been put into the buffer. Now you’re trying to get the reversed value out. If you do supply an argument, it should be the position in the buffer that you’re reading from — which is the beginning, or position 0. One way or another you need to tell the buffer that you’re going back to the beginning to read — there are several ways, but simply caling getDouble(0) works well:

    Since most of the ByteBuffer methods return the ByteBuffer itself as the return value, you can chain everything together if you like. This style may look a little weird to some people though. It’s your call.

    «I’m not back.» — Bill Harding, Twister

    Источник

    Оцените статью