Convert ascii to int? [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert ascii to int ??
int a=53
This is an ASCII value for 5 i want Convert it to Integer

Are you looking for something like this?
int asciiValue = 53;
int numericValue = Character.getNumericValue(asciiValue);
System.out.println(numericValue);

Related

how to convert OX12 to decimal [duplicate]

This question already has answers here:
Convert hex string to int
(9 answers)
Closed 2 years ago.
I need to convert hexadecimal value which is OX12 to decimal in JAVA,
as I knew hexadecimal is based 16 (1..9, a..f), but in this case, I don't know how to convert with that value.
Can someone help me?
Thanks so much
If the number is a String, you can use the parseInt method of the Integer class with first argument "12" (the number in hex) and second argument 16, the radix, or base, of the number
int number = Integer.parseInt("12", 16);
System.out.println(number);
// output is 18
If the number is not a String, you can simply do the following:
int number = 0x12;
System.out.println(number);
// output is 18

How can I Split the String Correctly? [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 3 years ago.
I am getting a result of "kjhvkjkasdfkjh#grouping#group_group.12018-08-20".
I am looking to split the string in java to only get the "2018-08-20".
Any suggestions? The part I want is always at the end.
String sample = "kjhvkjkasdfkjh#grouping#group_group.12018-08-20"
int SubstringStart = sample.length() - 10;
String outputsample = sample.substring(SubstringStart);
will break if string has length shorter than 10

How do you convert a character to its binary ASCII code in java? Also does the binary value have to be a String or a char? [duplicate]

This question already has answers here:
How can I get a Unicode character's code?
(7 answers)
Closed 5 years ago.
1.char chCharacter = 'A';
2.int iBinaryCode;
Storing the character in a char
Where I want to store the binary value
You can get a String with its binary value as follows
Integer a = Character.getNumericValue(chCharacter);
String binary = Integer.toBinaryString(a)
EDIT: and get it as an Integer doing:
Integer binaryInteger = Integer.valueOf(binary);

How do I put a user input int as array length in Java? [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 7 years ago.
If I ask the user for input, how do I put it as the length of an array?
Example:
String teamnum = Input.nextLine();
int teams[]=new int[teamnum];
You need to cast the input as a integer also that's not the way you declare arrays. Try this:
String teamnum = Input.nextLine();
int newnum = Integer.parseInt(teamnum);
int[] teams = new int[newnum];

NumberFormatException from hex number [duplicate]

This question already has answers here:
Convert hex string to int
(9 answers)
Closed 8 years ago.
In my code I have
int i = Integer.parseInt("f8004896",16);
when I run the program it throws a NumberFormatException
java.lang.NumberFormatException: For input string: "f8004896"
What am I doing wrong? This seems relatively straightforward but its not working.
long i = Long.parseLong("f8004896", 16);
System.out.println(i);
System.out.println(Integer.MAX_VALUE);
Output:
4160768150
2147483647
Decimal value for f8004896 is 4160768150 and it's more than 2^31-1 (upper limit for int type). So you should use long instead: Long.parseLong()

Categories