How to convert from int to int array in Java? [duplicate] - java

This question already has answers here:
Convert an integer to an array of digits
(24 answers)
Closed 2 years ago.
I would just like to ask how to convert an int to int array - for example:
int number = 12345;
to:
[1,2,3,4,5];
Btw - could not really find anything out there, so we may hope that someone know.
thanks in advance

You can do it like this:
int number = 12345;
int[] digits = String.valueOf(number).chars().map(c -> c-'0').toArray();
Explanation:
First convert int to string with String.valueOf(number) to be able to use the method chars() which get a stream of int that represents the ASCII value of each char in the string. Then, we use the map function map(c -> c-'0') to convert the ASCII value of each character to its value, subtracting the value of the ASCII code from the character '0' from the ASCII code of the actual character. Finally, the stream is converted to an array with toArray().

Related

How can I pad a hexadecimal digit with 0 in java? [duplicate]

This question already has answers here:
Integer to two digits hex in Java
(7 answers)
Closed 1 year ago.
I'm trying to pad a hexadecimal digit with 0's in the beginning so that the length of it is always 4. For example, the FF is going to be padded as 00FF. I have tried to use String.format("%04d", number) but it didn't work as my hexadecimal digit is actually a string.
I have also tried using StringUtils.leftPad() but for some reason, IntelliJ couldn't detect it.
Please note my count is going to change, but I haven't represented it here as I'm yet to implement it. This is my sample code. Thanks.
public static void main(String[] args) {
int count = 0;
String orderID = Integer.toHexString(count).toUpperCase();
System.out.println(String.format("%04d", Integer.valueOf(orderID)));
}
String.format("%04x", 255)
with output
00ff
(or use X for uppercases hex digits)
As #user16320675 point, more info about Formatter here.

Getting Integers from Char Array? [duplicate]

This question already has answers here:
How can I convert a char to int in Java? [duplicate]
(4 answers)
Java: parse int value from a char
(9 answers)
Closed 3 years ago.
I was experimenting with char arrays and integers, when I found something that confused me a lot. In Java, if you type the following code:
String inputs = "123";
char[] inputs2 = inputs.toCharArray(); // Turn String into Char Array
int a = inputs2[0];
println(a);
I would have expected for "a" to return 1 because 1 is the first element in the array. However, it returned 49. Why does this occur? Is there any way to get around this issue?

Converting BigInteger to String, and then back to BigInteger [duplicate]

This question already has answers here:
How to convert BigInteger to String in java
(9 answers)
Closed 6 years ago.
I have code which takes a BigInteger
48451322989516520225703217657832557994348537500303367742400825550923100192302069868489479191146175399344044876949227990959739850227034985347595351425385263774028421913031512265649684935654507691239234667482091135118571200215310568615906290473167269182601320011893758047720172195848415075205065039282385885704
And after I perform bigint.toString(16) I get this string:
44ff39b391fe68e6522d4e2fd99a6c5c77afdae691357f04e5e504790460e7a8e30b3d988e2c1ad316660af7d4e70c012ab711bb77a238f7c2281903523446677f3f26b5d7338c77939f9d97268125adf309aba85e9113f895e9d5179987ab02f3cc255c83e05579664cb08f79390373cb7cce5d280c6647091721567e029a08
which contains letters in it, so after I try and convert it back to a BigInteger I can't b/c it's telling me it's not a number b/c of the letters in the value.
How do you properly go from a BigInteger, to string, and then back to BigInteger? Here is what my code is:
BigInteger decryptedBI = resultBI.modPow(keyD, keyN); // my biginteger
String decrypted = decryptedBI.toString(16); // converted it to a string value
System.out.println(decryptedBI);
System.out.println(decrypted); // this is the decrypted hash
BigInteger has a constructor that takes in a String for the value and an int denoting the radix (in your case, 16).
See the API docs, but here's you how use it:
BigInteger newBI = new BigInteger(myString, 16);

Java: Problems converting char to int [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java char array to int
I have the following code, where stackCells is an ArrayList object:
int[] stackCell = {0, 0};
if (!stackCells.isEmpty()) {
String stackCellString = stackCells.get(0);
stackCell[0] = stackCellString.charAt(0);
stackCell[1] = stackCellString.charAt(1);
}
So the problem I am encountering is that stackCell is not interpreting the character as an int. For example, the value of stackCellString should be "88". However, when I use chartAt(0) and charAt(1), I get an int value of 56. If I use those same calls inside a System.out.println(stackCellString.charAt(0) + stackCellString.charAt(1)) command, I get the correct result (i.e. "88").
It's interpreting the character as an int in the following way:
56 is the ASCII code for the character '8'.
If you want to grab the numeric digits from the string, you will need to use something like Integer.parseInt() as one of the comments mentioned.
A more efficient solution might be to do some math based on the ASCII code. If you know all of the characters are decimal digits, you could do (code - 48) to get the digit (where code is the ASCII code). For instance, (56 - 48) = 8, which is what you want here.
56 is the ASCII code of the digit 8. To get what you want, you could do this:
stackCell[0] = stackCellString.charAt(0) - '0';
stackCell[1] = stackCellString.charAt(1) - '0';
String stackCellString="88";
stackCell[0] = stackCellString.charAt(0);
stackCell[1] = stackCellString.charAt(1);
System.out.println(stackCell[0]);
System.out.println(stackCell[1]);
System.out.println(stackCell[0]+stackCell[1]);
System.out.println(stackCellString.charAt(0) + stackCellString.charAt(1));
output:
56
56
112
112
You are not returning the charAt as its character representation.
If you want it to return "88" you need to do this:
stackCellString.subString(0,1) + stackCellString.subString(1,2);
Another work around is this:
"" + stackCellString.charAt(0) + stackCellString.charAt(1);

Cannot convert String to Integer in Java [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 5 years ago.
I have written a function to convert string to integer
if ( data != null )
{
int theValue = Integer.parseInt( data.trim(), 16 );
return theValue;
}
else
return null;
I have a string which is 6042076399 and it gave me errors:
Exception in thread "main" java.lang.NumberFormatException: For input string: "6042076399"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:461)
Is this not the correct way to convert string to integer?
Here's the way I prefer to do it:
Edit (08/04/2015):
As noted in the comment below, this is actually better done like this:
String numStr = "123";
int num = Integer.parseInt(numStr);
An Integer can't hold that value. 6042076399 (413424640921 in decimal) is greater than 2147483647, the maximum an integer can hold.
Try using Long.parseLong.
That's the correct method, but your value is larger than the maximum size of an int.
The maximum size an int can hold is 231 - 1, or 2,147,483,647. Your value is 6,042,076,399. You should look at storing it as a long if you want a primitive type. The maximum value of a long is significantly larger - 263 - 1. Another option might be BigInteger.
That string is greater than Integer.MAX_VALUE. You can't parse something that is out of range of integers. (they go up to 2^31-1, I believe).
In addition to what the others answered, if you have a string of more than 8 hexadecimal digits (but up to 16 hexadecimal digits), you could convert it to a long using Long.parseLong() instead of to an int using Integer.parseInt().

Categories

Resources