简体   繁体   English

使用字节指针使用JNA编写响应

[英]Using byte pointer for write a response using JNA

I'm trying to use a function in C using JNA: 我正在尝试使用JNA在C中使用一个函数:

C: C:

int addHoliday(unsigned char* data);

JAVA: JAVA:

int addHoliday(byte[] data);

I'm passing my byte[] with information, but in C I'm using the same pointer to write a response. 我将信息传递给byte [],但是在C语言中,我使用相同的指针来编写响应。 Can I catch the same byte[] with the new information? 我可以使用新信息捕获相同的byte []吗?

只需将其作为字节指针数组即可:

int addHoliday(byte* data);

Yes, primitive arrays work just like memory buffers when used in a direct function call. 是的,在直接函数调用中使用时,原始数组的工作方式与内存缓冲区一样。 The native code will see a consistent buffer for the duration of the native call, and your Java code will see in the byte[] whatever data was written by the native code. 在本机调用期间,本机代码将看到一个一致的缓冲区,而您的Java代码将在byte[]看到由本机代码写入的任何数据。

As for the signedness of the data, any unsigned char elements with the high bit set will appear as negative values in the byte[] in Java. 至于数据的有unsigned char ,任何设置了高位的unsigned char元素在Java中的byte[]中都将显示为负值。 To properly extract the data, you'll need to mask out the higher bits, eg 为了正确提取数据,您需要屏蔽掉较高的位,例如

int unsigned_value = (int)byte_value & 0xFF;

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

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