Question

the result of my decryption routine returns a byte array like this:

30303030303030310000000000000000

That should be converted to (int) 00000001, but I cannot get rid of the trailing 0x00's. What would be the best way to accomplish that?

Any help would be greatly appreciated, thanks!

Was it helpful?

Solution

Found the workaround for trimming the trailing 0x00's in the byte array (replace or trim won't work):

public static String bytesToString(byte[] data) {
    String dataOut = "";
    for (int i = 0; i < data.length; i++) {
        if (data[i] != 0x00)
            dataOut += (char)data[i];
    }
    return dataOut;
}

OTHER TIPS

Why not using the replace method?

String foo = new String(myBytes);
foo = foo.replace((char)0, ''); // Character with zero

Integer result = Integer.valueOf(foo);

Or even better, a Regular Expression that filters the String, getting rid of anything out of the 0-9 range.

String foo = new String(myBytes);
foo = foo.replace((char)0, ''); // Character with zero

Integer result = Integer.valueOf(foo);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top