我将此字节数组保存为数据库中的字符串:
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
};
现在我想从我的数据库获取这个字符串并将其转换为字节数组(如上所示)。 我尝试这样做:
String[] str_pass = obj.getString("password").split(" ");
byte[] NEW_KEY = convertToBytes(str_pass);
但是这个convertToBytes函数就是问题所在:
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;
}
有更好的方法吗?
最佳答案
字节数组只是一个字符串,因此您应该使用二维字节数组
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;
}
关于java - 将字符串数组转换为字节数组(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48962084/