سؤال

Is there an equivalent of ntohll C++ function in Java?

The reference of ntohll can be found here: ntohll function.

The thing is I need to convert a 64 bits long from TCP/IP network order to little endian long.

هل كانت مفيدة؟

المحلول

java equivalent function of ntohll is: long is equivalent of 64 bit

    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).

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. If you are using JDK 1.5 or higher, you can use the reverseBytes method for the integer objects:

long data = Long.reverseBytes(some_data);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top