문제

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