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!

有帮助吗?

解决方案

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

其他提示

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top