集册 Java实例教程 设置ByteBuffer的字节顺序

设置ByteBuffer的字节顺序

欢马劈雪     最近更新时间:2020-01-02 10:19:05

559
设置ByteBuffer的字节顺序

import java.nio.ByteBuffer;

import java.nio.ByteOrder;


public class Main {
/*
n o w  j a v a  . c o m
*/

  public static void main(String[] args) throws Exception {

    // Obtain a ByteBuffer; see also Creating a ByteBuffer

    ByteBuffer buf = ByteBuffer.allocate(10);


    // Get default byte ordering

    ByteOrder order = buf.order(); // ByteOrder.BIG_ENDIAN


    // Put a multibyte value

    buf.putShort(0, (short) 123);// 来自 时代Java公众号

    buf.get(0); // 0

    buf.get(1); // 123


    // Set to little endian

    buf.order(ByteOrder.LITTLE_ENDIAN);


    
展开阅读全文