简体   繁体   English

Java相当于ntohll函数

[英]Java equivalent of ntohll function

Is there an equivalent of ntohll C++ function in Java? Java中是否有与ntohll C ++函数等效的函数?

The reference of ntohll can be found here: ntohll function . ntohll的参考可以在这里找到: ntohll function

The thing is I need to convert a 64 bits long from TCP/IP network order to little endian long. 关键是我需要将64位长的TCP / IP网络顺序转换为小端长的顺序。

java equivalent function of ntohll is: long is equivalent of 64 bit ntohll的java等效功能是:long等效于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 uses network byte order already, so there is no need to convert them (which is why these functions do not exist in Java). Java已经使用了网络字节顺序,因此无需转换它们(这就是Java中不存在这些功能的原因)。

Update 更新资料

Since you are reading a file that is in little endian bit patterns, you have to write your own (or use a library) if you are using JDK < 1.5. 由于您正在读取的文件格式为小尾数位格式,因此,如果使用的JDK <1.5,则必须编写自己的文件(或使用库)。 If you are using JDK 1.5 or higher, you can use the reverseBytes method for the integer objects: 如果使用的是JDK 1.5或更高版本,则可以对整数对象使用reverseBytes方法:

long data = Long.reverseBytes(some_data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM