1

I have an unint8 byte array which a function from a library is returning the library is cross compiled from golang using gomobile, Golang has byte arrays of type unint8,

The byte array is

[4, 19, 35, 76, -77, -6, 106, -70, -95, -37, -58, 2, 20, 94, 34, -73, 79, 69, -84, -90, 30, 27, 125, -102, -116, 105, 52, 89, -62, 116, -92, 27, -56, 98, -124, 42, -2, -109, -30, -101, -60, -12, -103, 28, 26, 46, -54, -33, 61, -17, 115, 39, -14, -15, -60, -109, -119, -106, -128, 95, 65, 84, 12, -56, -76]

I want to convert this byte array to string in java

I know you can convert byte to string using new String()

but when I convert the byte array I get random characters as

♦‼#L��j����☻¶^\"�OE��▲��i4Y�t�b�*�����∟→.��=�s'��ē���_AT♀ȴ

4
  • did you check what was the input string? Commented Apr 1, 2020 at 7:21
  • The input string is a public key derived from a key pair which can be as d5f8b59cc5606f653083c14458284328f46c91f98be3a5cc6521052e7b3c1c2d and the string whose byte array is generated can be as 0x04127a7a32bc31691052781ec6b37ebfe841d9ca12b08fb5ebd8fb391d437dad2f70fe367104006418e846577866428ea61dc2e720799ecf2dce0affcabfe5749e Commented Apr 1, 2020 at 7:34
  • String input = "d5f8b59cc5606f653083c14458284328f46c91f98be3a5cc6521052e7b3c1c2d"; byte[] bytes = input.getBytes(); System.out.println(Arrays.toString(bytes)); String output = new String(bytes); System.out.println(output); // prints d5f8b59cc5606f653083c14458284328f46c91f98be3a5cc6521052e7b3c1c2d there is something else n between like some other transformation Commented Apr 1, 2020 at 8:26
  • this is the byte array for input string [100, 53, 102, 56, 98, 53, 57, 99, 99, 53, 54, 48, 54, 102, 54, 53, 51, 48, 56, 51, 99, 49, 52, 52, 53, 56, 50, 56, 52, 51, 50, 56, 102, 52, 54, 99, 57, 49, 102, 57, 56, 98, 101, 51, 97, 53, 99, 99, 54, 53, 50, 49, 48, 53, 50, 101, 55, 98, 51, 99, 49, 99, 50, 100] using above method Commented Apr 1, 2020 at 8:27

1 Answer 1

1

I am not sure, what is your expected output. This function would print out something like:

010203041B1A

public class ByteUtils {

private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
private static final int MASK_BYTE_SIZE = 0xFF;
private static final int MASK_SECOND_TUPLE = 0x0F;
private static final int SHIFT_FIRST_TUPLE = 4;

public static String bytesToHexString(final byte... bytes) {
    if (bytes == null || bytes.length == 0) {
        return "";
    }
    final char[] hexChars = new char[bytes.length * 2];
    for (int j = 0; j < bytes.length; j++) {
        int v = bytes[j] & MASK_BYTE_SIZE;
        hexChars[j * 2] = HEX_ARRAY[v >>> SHIFT_FIRST_TUPLE];
        hexChars[j * 2 + 1] = HEX_ARRAY[v & MASK_SECOND_TUPLE];
    }
    return new String(hexChars);
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

My output should be something as 0x04127a7a32bc31691052781ec6b37ebfe841d9ca12b08fb5ebd8fb391d437dad2f70fe367104006418e846577866428ea61dc2e720799ecf2dce0affcabfe5749e
Basically this code should work for you. Maybe you need to adopt the HEX_ARRAY variable using non-capital letters.

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.