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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.