Java等效于NTOHLL函数

Java equivalent of ntohll function

本文关键字:NTOHLL 函数 Java      更新时间:2023-10-16

java中的 ntohll c 功能等效吗?

可以在此处找到ntohll的引用:ntohll函数。

问题是我需要将64位从TCP/IP网络订单转换为Little Endian。

ntohll的java等效函数是:长等效于64位

    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
   public long ntohll(long convert)
    {
            ByteBuffer bbuf = ByteBuffer.allocate(8);  
            bbuf.order(ByteOrder.BIG_ENDIAN);  
            bbuf.putLong(convert);  
            bbuf.order(ByteOrder.LITTLE_ENDIAN);  
            return bbuf.getLong(0); 
    }

java已经使用了网络字节顺序,因此无需转换它们(这就是为什么这些功能在Java中不存在的原因)。

update

,由于您正在阅读一个小末日位模式的文件,因此,如果您使用的是JDK<1.5。如果使用JDK 1.5或更高版本,则可以将reverseBytes方法用于整数对象:

long data = Long.reverseBytes(some_data);