[英]reading long bytes from c in java
I have to send a long from my c program to be read in java code using a byte buffer . 我必须从我的c程序中发送一个长字节,以便使用字节缓冲区在java代码中进行读取。 I am doing something wrong as i cant send 1234 and read it back in java as it gets corrupted to 564049465049088.
我做错了什么,因为我无法发送1234并在Java中读回它,因为它已损坏为564049465049088。
In my C code I do 在我的C代码中
int64_t resultl = 1234;
Then to copy 然后复制
uint8_t * outBuffer <== pointer to the buffer;
memcpy(outBuffer,&resultl,8);
In java I read back the bytes as 在java中,我读回字节为
ByteBuffer buf = ByteBuffer.wrap(bytes);
offset=0;
long l = buf.getLong(offset);
This is giving me wrong value 564049465049088 as i had sent 1234 from C; 这是错误的值564049465049088,因为我从C发送了1234; Can someone please suggest what I am doing wrong & how can i correct the same.
有人可以建议我做错了什么,我该如何纠正。
You have to change the endianness of the ByteBuffer: 您必须更改ByteBuffer的字节序:
ByteBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
since you are probably on a little-endian computer system (x86 and most ARM systems). 因为您可能使用的是低端计算机系统(x86和大多数ARM系统)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.