[英]How to convert String to Integer array and back in JAVA
我想将String反序列化为int []数组并返回。 我尝试这样的事情
public static int[] getIntArrayFromString(String string, int stringMaxLenght) {
byte[] bytes = string.getBytes();
int rest = bytes.length % 4;
int times = (bytes.length - rest) / 4;
int[] result = new int[stringMaxLenght];
int maxIndex = 0;
for (int i = 0; i < times; i++) {
if (times > stringMaxLenght)
break;
int in = createIntFromBytes(bytes[i * 4 + 0], bytes[i * 4 + 1], bytes[i * 4 + 2],
bytes[i * 4 + 3]);
result[i] = in;
maxIndex = i;
}
byte[] restb = new byte[4];
for (int i = 0; i < rest; i++) {
restb[i] = bytes[(maxIndex + 1 * 4) + i];
}
if (times < stringMaxLenght) {
int lastInt = createIntFromBytes(restb);
result[maxIndex + 1] = lastInt;
}
return result;
}
public static int createIntFromBytes(byte byte0, byte byte1, byte byte2, byte byte3) {
byte[] byteArray = new byte[4];
byteArray[0] = byte0;
byteArray[1] = byte1;
byteArray[2] = byte2;
byteArray[3] = byte3;
return createIntFromBytes(byteArray);
}
public static String getStringFromIntegerArray(int[] intArray) {
ByteBuffer buffer = ByteBuffer.allocate(intArray.length * 4);
for (int integer : intArray) {
byte[] bs = createBytesFromInt(integer);
buffer.put(bs);
}
return getStringFromBytes(buffer.array());
}
public static int createIntFromBytes(byte[] bytes) {
return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getInt();
}
public static String getStringFromBytes(byte[] bytes) {
String string = new String(bytes);
return string.trim();// otherwise it creates string with empty chars
}
但似乎不适用于例如字符串“ Signature”。 您有任何想法吗,我做错了什么,或者应该如何做得更好。
public static void main(String[] args) {
int[] test = {1,2,3,4,5,6,7,8};
// From int[] to String
String input = Arrays.toString(test);
// Test
System.out.println(input);
// String back to int[]
test = toIntArray(input);
// Test
input = Arrays.toString(test);
System.out.println(input);
}
public static int[] toIntArray(String input) {
String beforeSplit = input.replaceAll("\\[|\\]|\\s", "");
String[] split = beforeSplit.split("\\,");
int[] result = new int[split.length];
for (int i = 0; i < split.length; i++) {
result[i] = Integer.parseInt(split[i]);
}
return result;
}
结果:
[1、2、3、4、5、6、7、8]
[1、2、3、4、5、6、7、8]
How to convert String to Integer array and back in JAVA
尽管我不清楚您问题的内容,但是假设您想知道内置函数是否可用,要将int
数组转换为字符串,可以使用Arrays.toString
函数,例如:
String strArr = Arrays.toString(new int[]{112, 12 ,13});
System.out.println(strArr);
它将创建以下类型的输出格式:
[112, 12, 13]
问题出在这一行。 索引偏移量很差。
错误: restb [i] =字节[(maxIndex + 1 * 4)+ i];
好: restb [i] =字节[(maxIndex * 4)+ i + 4];
因此,它可能对某人有所帮助。 该代码现在运行良好。
public static int[] getIntArrayFromString(String string, int stringMaxLenght) {
byte[] bytes = string.getBytes();
int rest = bytes.length % 4;
int times = (bytes.length - rest) / 4;
int[] result = new int[stringMaxLenght];
int maxIndex = 0;
for (int i = 0; i < times; i++) {
if (times > stringMaxLenght)
break;
int in = createIntFromBytes(bytes[i * 4 + 0], bytes[i * 4 + 1], bytes[i * 4 + 2],
bytes[i * 4 + 3]);
result[i] = in;
maxIndex = i;
}
byte[] restb = new byte[4];
for (int i = 0; i < rest; i++) {
restb[i] = bytes[(maxIndex * 4) + i + 4];
}
if (times < stringMaxLenght) {
int lastInt = createIntFromBytes(restb);
result[maxIndex + 1] = lastInt;
}
return result;
}
public static int createIntFromBytes(byte byte0, byte byte1, byte byte2, byte byte3) {
byte[] byteArray = new byte[4];
byteArray[0] = byte0;
byteArray[1] = byte1;
byteArray[2] = byte2;
byteArray[3] = byte3;
return createIntFromBytes(byteArray);
}
public static String getStringFromIntegerArray(int[] intArray) {
ByteBuffer buffer = ByteBuffer.allocate(intArray.length * 4);
for (int integer : intArray) {
byte[] bs = createBytesFromInt(integer);
buffer.put(bs);
}
return getStringFromBytes(buffer.array());
}
public static int createIntFromBytes(byte[] bytes) {
return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getInt();
}
public static String getStringFromBytes(byte[] bytes) {
String string = new String(bytes);
return string.trim();// otherwise it creates string with empty chars
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.