0

I saved this byte array as a string in my database :

public static final byte[] NEW_KEY_2KTDES = {
    (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
    (byte)0x00, (byte)0x00, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
    (byte)0xff
};

Now I want to get this string from my db and convert it to a byte array (like above). I tried to do it :

String[] str_pass = obj.getString("password").split(" "); 
byte[] NEW_KEY = convertToBytes(str_pass);

But that convertToBytes function is the problem :

private static byte[] convertToBytes(String[] strings) {
    byte[] data = new byte[strings.length];
    for (int i = 0; i < strings.length; i++) {
        String string = strings[i];
        data[i] = string.getBytes(Charset.defaultCharset()); // you can chose charset
    }
    return data;
}

Is there a better way ?

0

2 Answers 2

3

an array of bytes is just one String so you should use a two dimensional byte array

private static byte[][] convertToBytes(String[] strings) {
    byte[][] data = new byte[strings.length][];
    for (int i = 0; i < strings.length; i++) {
        String string = strings[i];
        data[i] = string.getBytes(Charset.defaultCharset()); // you can chose charset
    }
    return data;
}
Sign up to request clarification or add additional context in comments.

4 Comments

But I want a byte[]
it will return byte array of every element in the string array
My target is getting a byte[] array of whole string. Each element of string is a byte.
Convert whole array into one string first using join and then use string.getBytes()
1

Change your bytes array to Hex String and save to database, and convert Hex String to bytes array back:

public class HexUtils {
    private static final char[] HEX_ARRAY = {'0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public static String encodeWithHex(@NonNull byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int i = 0, j = 0; i < bytes.length; i++, j += 2) {
            int v = bytes[i] & 0xFF;
            hexChars[j] = HEX_ARRAY[v >>> 4];
            hexChars[j + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static byte[] fromHexString(@NonNull String hexStr) {
        hexStr = hexStr.replace(" ", ""); // support spaces
        if (hexStr.length() % 2 != 0) {
            throw new IllegalArgumentException("Invalid HEX string " + hexStr);
        }

        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < result.length; i++) {
            int high = fromHexChar(hexStr, i * 2) << 4;
            int low = fromHexChar(hexStr, i * 2 + 1);
            result[i] = (byte) ((high | low) & 0xFF);
        }
        return result;
    }

    private static int fromHexChar(String hexStr, int index) {
        char ch = hexStr.charAt(index);
        if (ch >= '0' && ch <= '9') {
            return ch - '0';
        } else if (ch >= 'a' && ch <= 'f') {
            return 10 + (ch - 'a');
        } else if (ch >= 'A' && ch <= 'F') {
            return 10 + (ch - 'A');
        } else {
            throw new IllegalArgumentException("Invalid HEX string: " + hexStr);
        }
    }

}

1 Comment

Nice job, I used encodeWithHex to save to my db and used fromHexString to read from db.

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.