将字符串数组转换为字节数组(java)
我将此字节数组保存为数据库中的字符串:将字符串数组转换为字节数组(java),java,android,arrays,byte,Java,Android,Arrays,Byte,我将此字节数组保存为数据库中的字符串: 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)0x
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;
}
专用静态字节[]转换字节(字符串[]字符串){
字节[]数据=新字节[strings.length];
for(int i=0;i
有更好的方法吗?将字节数组更改为十六进制字符串并保存到数据库,然后将十六进制字符串转换回字节数组:
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);
}
}
}
公共类HexUtils{
私有静态final char[]十六进制数组={'0','1','2','3','4','5','6',
‘7’、‘8’、‘9’、‘A’、‘B’、‘C’、‘D’、‘E’、‘F’};
公共静态字符串encodeWithHex(@NonNull byte[]bytes){
char[]hexChars=新字符[bytes.length*2];
对于(int i=0,j=0;i>>4];
hexChars[j+1]=十六进制数组[v&0x0F];
}
返回新字符串(hexChars);
}
HEXSTRING(@NonNull String hexStr)中的公共静态字节[]{
hexStr=hexStr.replace(“,”);//支持空格
如果(hexStr.length()%2!=0){
抛出新的IllegalArgumentException(“无效的十六进制字符串”+hexStr);
}
字节[]结果=新字节[hexStr.length()/2];
for(int i=0;i int high=fromHexChar(hexStr,i*2)='0'&&ch='a'&&ch='a'&&ch将字节数组更改为十六进制字符串并保存到数据库,然后将十六进制字符串转换回字节数组:
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);
}
}
}
公共类HexUtils{
私有静态final char[]十六进制数组={'0','1','2','3','4','5','6',
‘7’、‘8’、‘9’、‘A’、‘B’、‘C’、‘D’、‘E’、‘F’};
公共静态字符串encodeWithHex(@NonNull byte[]bytes){
char[]hexChars=新字符[bytes.length*2];
对于(int i=0,j=0;i>>4];
hexChars[j+1]=十六进制数组[v&0x0F];
}
返回新字符串(hexChars);
}
HEXSTRING(@NonNull String hexStr)中的公共静态字节[]{
hexStr=hexStr.replace(“,”);//支持空格
如果(hexStr.length()%2!=0){
抛出新的IllegalArgumentException(“无效的十六进制字符串”+hexStr);
}
字节[]结果=新字节[hexStr.length()/2];
for(int i=0;i int high=fromHexChar(hexStr,i*2)='0'&&ch='a'&&ch='a'&&ch字节数组只是一个字符串,因此您应该使用二维字节数组
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;
}
专用静态字节[][]转换为字节(字符串[]字符串){
字节[][]数据=新字节[strings.length][];
for(int i=0;i
字节数组只是一个字符串,因此您应该使用二维字节数组
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;
}
专用静态字节[][]转换为字节(字符串[]字符串){
字节[][]数据=新字节[strings.length][];
for(int i=0;i
但是我想要一个byte[]它将返回字符串数组中每个元素的byte数组。我的目标是得到一个整个字符串的byte[]数组。字符串的每个元素都是一个字节。首先使用join
将整个数组转换成一个字符串,然后使用string.getBytes()
但是我想要一个byte[]它将返回字符串数组中每个元素的字节数组。我的目标是获取整个字符串的字节[]数组。字符串的每个元素都是一个字节。首先使用join
将整个数组转换为一个字符串,然后使用string.getBytes()
干得好,我用encodeWithHex保存到我的数据库,用fromHexString读取数据库。干得好,我用encodeWithHex保存到我的数据库,用fromHexString读取数据库。