简体   繁体   English

如何将String值转换为Pointer <Byte> (Java,JNAerator)?

[英]How to convert String value to Pointer<Byte> (Java, JNAerator)?

I have converted .dll library to JAR by using JNAerator. 我已使用JNAerator将.dll库转换为JAR。 Now I need to call OpenTCPIPPort_V method which looks like this: public static short OpenTCPIPPort_V(Pointer<Byte > tcpPort, Pointer<Byte > IPAddr) 现在我需要调用OpenTCPIPPort_V方法,如下所示: public static short OpenTCPIPPort_V(Pointer<Byte > tcpPort, Pointer<Byte > IPAddr)

How to pass two string values to this method? 如何将两个字符串值传递给此方法?

I found solution. 我找到了解决方案 This is method Pointer<Byte> org.bridj.Pointer.pointerToCString(String string) 这是方法Pointer<Byte> org.bridj.Pointer.pointerToCString(String string)

I tried pointerToCString for the TekVisa DLL (DLL to control measurement instruments from Tektronix) which did not work for opening an instrument session. 我为TekVisa DLL(用于控制Tektronix的测量仪器的DLL)尝试了pointerToCString,这对于打开仪器会话不起作用。 The second answer in this thread is better but misses a detail: The byte array has to be terminated with a 0. My code below works. 这个帖子中的第二个答案更好但是错过了一个细节:字节数组必须以0终止。我的代码可以工作。 (The instrument variable is of type String and contains the instrument string, for instance "TCPIP::::INSTR".) (仪器变量的类型为String,包含仪器字符串,例如“TCPIP :::: INSTR”。)

Interestingly, pointerToCString works when sending a command to the instrument, for instance viWrite("*IDN?"). 有趣的是,pointerToCString在向仪器发送命令时起作用,例如viWrite(“* IDN?”)。

  Pointer<Byte> pViString = Pointer.allocateBytes(instrument.length() + 1);
  byte[] instrumentBytes = instrument.getBytes();
  pViString.setBytes(instrumentBytes);
  pViString.setByteAtIndex(instrument.length(), (byte) 0);

There is a function from a String class called getBytes. String类中有一个名为getBytes的函数。 Here is an example: 这是一个例子:

String example = "example, string";
byte[] bytes = example.getBytes();

What you need now is to put those byte[] values into your's Pointer. 你现在需要的是将那些byte []值放入你的指针中。 I think you can handle now. 我想你现在可以办理了。

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

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