如何在JAVA中将String转换为Integer数组并返回

时间:2013-12-08 13:53:31

标签: java string serialization integer

我想将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”。你有什么想法,我做错了什么,或者应该如何做得更好。

3 个答案:

答案 0 :(得分:3)

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]

答案 1 :(得分:0)

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]

答案 2 :(得分:0)

问题出在这一排。索引偏移量很差。

错误: restb [i] = bytes [(maxIndex + 1 * 4)+ i];

好: restb [i] = bytes [(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
}