Getting Integers from Char Array? [duplicate] - java

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?

Related

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

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().

How to convert binary string to a byte? [duplicate]

This question already has answers here:
java: convert binary string to int
(4 answers)
Closed 6 years ago.
there is a string fill with 0 and 1,like String s = "10000000", which length is 8.And how can i transform it to a byte.such as "10000000"===>-128.
I try to use Byte.parseByte(s, 2),but get the error "Value out of range. Value:"10000000" Radix:2".So,how can i solve it.
You need to parse it as an Integer and then cast it to byte:
...
String s = "10000000";
int val = Integer.parseInt(s, 2);
byte b = (byte) val;
System.err.println(b);
...
Output:
-128

Fixing Disappearing Zeros [duplicate]

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 6 years ago.
I wanted to divide the integer and store it in an array
For Ex:1000000000000 into two indexes
arr[0]=1000000
arr[1]=000000
but arr[1] stores it as 0 instead of 0000000.
I wanted to perform some operations with it,so i needed 7 zeros in it ,instead of 1 zero.
Is it achievable in some way ?
Convert your number to a string then use substring() to split it up.
long num = 1000000000000L;
String str = num + "";
String[] array = new String[2];
array[0] = str.substring(0, 6);
array[1] = str.substring(7);

Juxtapose number in Int [duplicate]

This question already has answers here:
How to concatenate int values in java?
(22 answers)
Closed 7 years ago.
I'm a beginner in Java. I have a simple question :
int EPSGcode = 0;
int coordinateReferenceSystem = 326;
int fuseauUTM_l = 30;
I would like to juxtapose "coordinateReferenceSystem" and "fuseauUTM_l" in ESPGcode.
I get EPSGcode = 356, but I want EPSGcode = 32630...
Simple question, any ideas ?
Concatenate two numbers as a String and parse that String back to int.
EPSGCode = Integer.parseInt(""+coordinateReferenceSystem+fuseauUTM_1);

i++ still working for immutable Integer in Java? [duplicate]

This question already has answers here:
Is Integer Immutable
(12 answers)
Closed 9 years ago.
I know Integer is immutable in Java. But I tried this:
Integer i = 4;
i++;
System.out.println(i); // output is 5
Why the self-increment is still working? Did Java create a new Integer object?
i++;
is equivalent to:
i = i + 1;
which i now refers to a different Integer object (5).

Categories

Resources