These methods are for JNI (Java Native Interface).

//================ convert the unsigned byte (written in C) to integer =====
//----- to save the memory, short or char can be used
public static int convertGetUnsigned(byte input) {
	return ((int) input & 0x000000FF);
}
 
//----- not tested
public static long convertGetUnsigned(int input) {
	return ((long) input & 0xFFFFFFFFL);
}
 
//=== convert from a byte array (written in C as unsigned int) to long =====
public static long convertByteArrayToUnsingedInt(byte[] buf, int offset) {
	int firstByte = (0x000000FF & ((int) buf[offset]));
	int secondByte = (0x000000FF & ((int) buf[offset + 1]));
	int thirdByte = (0x000000FF & ((int) buf[offset + 2]));
	int fourthByte = (0x000000FF & ((int) buf[offset + 3]));
	//----- blow commented line is wrong
	//long result = ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
	long result = ((long) (firstByte | secondByte << 8 | thirdByte << 16 | fourthByte << 24)) & 0xFFFFFFFFL;
	return result;
}


Back to JavaHowTo

Alumni Liaison

Meet a recent graduate heading to Sweden for a Postdoctorate.

Christine Berkesch