Java ByteBuffer put buffer in little endian byte order

Description

Java ByteBuffer put buffer in little endian byte order

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();//from   ww w .  j a  v a 2 s.com
    System.out.println("Byte Order: " + bb.order());
    while (bb.hasRemaining()) {
      System.out.print(bb.get() + "  ");
    }
    System.out.println();

    bb.clear();
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putShort((short) 300);
    bb.flip();
    System.out.println("Byte Order: " + bb.order());
    while (bb.hasRemaining()) {
      System.out.print(bb.get() + "  ");
    }
    System.out.println();
  }
}



PreviousNext

Related