Reputation: 3689
I'm trying to convert library from C++ to Java. I have a problem with pointer..
The code from C++ is:
int DECL2 daveSetBit(daveConnection * dc,int area, int DB, int byteAdr, int bitAdr) {
int a=1;
return daveWriteBits(dc, area, DB, 8*byteAdr+bitAdr, 1, &a);
}
and daveWriteBits declaration is:
daveWriteBits(daveConnection * dc,int area, int DB, int start, int len, void * buffer)
How can I convert this into java? I don't know how to convert pointer &a into java language..
public int SetBits(
int area,
int DBnum,
int byteAdr,
int bitAdr) {
int a=1;
return writeBits(area, DBnum, 8*byteAdr+bitAdr, 1, ??? )
}
UPDATE:
I forgot to add part of WriteBits function in Java..
public int writeBits(
int area,
int DBnum,
int start,
int len,
byte[] buffer) {
int errorState = 0;
semaphore.enter();
PDU p1 = new PDU(msgOut, PDUstartOut);
p1.prepareWriteRequest();
p1.addBitVarToWriteRequest(area, DBnum, start, len, buffer);
errorState = exchange(p1);
if (errorState == 0) {
PDU p2 = new PDU(msgIn, PDUstartIn);
p2.setupReceivedPDU();
if (p2.mem[p2.param + 0] == PDU.FUNC_WRITE) {
if (p2.mem[p2.data + 0] == (byte) 0xFF) {
if ((Nodave.Debug & Nodave.DEBUG_CONN) != 0)
System.out.println("writeBytes: success");
semaphore.leave();
return 0;
}
} else {
errorState |= 4096;
}
}
semaphore.leave();
return errorState;
}
Upvotes: 0
Views: 537
Reputation: 8027
Are you more familar with C++ or Java? The C++ code is treating an integer as an array of bytes. You can't do that directly in Java (I think) but you can do this
ByteArrayOutputStream buf = new ByteArrayOutputStream();
new DataOutputStream(buf).write(a);
return writeBits(area, DBnum, 8*byteAdr+bitAdr, 1, buf );
Converts an integer to an array of bytes which you can then pass to writeBits. Untested code.
Upvotes: 0
Reputation: 18344
It depends on how daveWriteBits
uses buffer
. If it is just treated as a simple integer, and if daveSetBit
does not use the value of a
after this call, just make it an int
. No pointers/references.
If daveSetBit
needs the value of a
(after any potential modifications in daveWriteBits
, then making a an Integer
may help.
If there is some really fancy pointer stuff going on (treating &a
itself as an address etc) then you may have to do a bit more design.
Upvotes: 0
Reputation: 849
Java doesn't have pointers. It has references:
c++:
Object *a=new Object();
java:
Object a=new Object();
Upvotes: 0
Reputation: 533492
Since the variable is ignored I would delete it.
However if it were not ignored you could sue
int[] a = { 1 };
writeBits(area, DBnum, 8*byteAdr+bitAdr, 1, a ) ;
// do something with a[0];
Upvotes: 1