0

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;
    }
1

4 Answers 4

1

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];
Sign up to request clarification or add additional context in comments.

Comments

0

Java doesn't have pointers. It has references:

c++:

Object *a=new Object();

java:

Object a=new Object();

Comments

0

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.

Comments

0

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.

3 Comments

probably better to manually split the integer than to use DataOutputStream... that or use ByteBuffer the whole time.
@veer: Sure, the important point is that the equivalent in Java is a conversion to byte[]
@Elfoc new byte[] { 0, 0, 0, 1 } assuming it expects it big-endian like Java is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.