Home:ALL Converter>Converting String to int in java

Converting String to int in java

Ask Time:2013-03-15T20:55:51         Author:Amar C

Json Formatter

I need to convert a string to a corresponding int array in java. i wrote the following code but its not working as i expected .

        String temp= "abc1";
        int[] intArray = new int[temp.length()];
        for (int i = 0; i < temp.length(); i++) {
            intArray[i] = Integer.parseInt(temp[i]);

I have written an rc4 encryption program which take the key and plain text as int arrays. So I need to convert the user specified key to int array before passing it to the encryption function. Is this the correct way of using key in encryption programs?

Author:Amar C,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15433085/converting-string-to-int-in-java
mprivat :

Use this to get the ASCII code\n\nintArray[i] = (int)temp.charAt(i);\n",
Bhavesh Shah :

You can convert string to charArray. Travesing charArray you can convert as:\n\nchar[] c = inputString.toCharArray()\nfor(int i=0;i<c.length;i++)\n int n = Integer.parseInt(c[i]);\n",
Deepesh Kumar :

if you are Using Java8 or higher then it might be helpful for you\n\nString temp=\"abc1\";\nint[] intArray =temp.chars().map(x->x-'0').toArray();\nSystem.out.println(Arrays.toString(intArray ));\n\n\nint array would be [49, 50, 51, 1]",