byte[] to long in JNI

koding :

I have a function to convert byte[] to long in java like this. The length of the array (byte[]) can be 4, 6 or 8 bytes.

public long toLong(byte[] dtIn) {
    try {
        return Long.parseLong(new String(dtIn).trim());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return -1;
}

Is there a function that is the same as the function above in JNI (C language).?

koding :
jlong toLong(jbyte *dtIn) {
    return strtol((const char*)dtIn, NULL, 10);
}

Example:

jbyte din[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38};
return toLong(din);

Result: 12345678 (same with java code)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=319759&siteId=1