Get ASCII from char from string [duplicate] - java

This question already has answers here:
Get ASCII value at input word
(6 answers)
Closed 8 years ago.
I have a loop with i incrementing through a string, and I want to print that character, but its ASCII code.
myString = "90210";
for (int i = 0; i < myString.length(); i++) {
System.out.println(myString.charAt(i));
}
To output:
57
48
50
49
48
Obviously charAt() doesn't do this, but I need another method to append to it to get my desired result.

Just cast to an int so that you get the numerical value of the char, which consequently uses the overloaded println(int) method
System.out.println((int)myString.charAt(i));

System.out.println(myString.charAt(i) - '\0');
System.out.println(myString.charAt(i) - 0);

Related

How to reconvert from int to AScII in java? [duplicate]

This question already has answers here:
Convert int to char in java
(18 answers)
Closed 1 year ago.
So what i understand is that if for example i have an int a = 49 and want to find out the ASCII equivalent to it, all i need to do is:
int a = 49;
System.out.println((char)a);
which has the Output: 1.
But how can i do this reversed? So lets say i have int a = 1 and i want the output to be 49?
I have already tried stuff like:
int a = 1;
System.out.println ((char)a);
But the output here is "." instead of 49.
This:
int a = 49;
System.out.println((char)a);
gives you the char whose internal value is 49, which is to say the unicode character '1'. There is no numerical conversion going on, it's just two ways to look at the same number.
In this:
int a = 1;
System.out.println((char)a);
you get the char whose internal value is 1, which is a unicode control character (ctrl/A, which probably won't be printed in any useful way). There is nothing there that can magically come up with the value 49.
If you had the character '1' then it's easy:
int a = '1';
System.out.println(a);
but this is practically the same as your first case; '1' and 49 are the same value.

How to count comma in a String java [duplicate]

This question already has answers here:
How do I count the number of occurrences of a char in a String?
(48 answers)
Closed 2 years ago.
I have a String, which contains a lot of commas.
I want to count all the commas in the String but I don't know how.
I'm using (split(",")-1)
But the problem is that if I input a String like this: One,Two,Three,Four,,,
Then it returns only 3 while I want it to be 6.
I think it is because split(",") returns a String[] that does not include null or empty values.
Is there any way to solve this problem?
One straightforward way would be to just compare the length of the input against the input with all commas removed:
String input = "One,Two,Three,Four,,,";
int numCommas = input.length() - input.replace(",", "").length();
System.out.println(numCommas); // prints 6

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

Adding three different char in java as java variable and getting a number [duplicate]

This question already has answers here:
In Java, is the result of the addition of two chars an int or a char?
(8 answers)
Closed 4 years ago.
IntelliJ IDEA Capture
Why i am getting 152, I think it will give me an error.
Please explain it.
public class character {
public static void main(String[] args) {
char myCharValue1 = 'A';
char myCharValue2 = '2';
char myCharValue3 = '%';
System.out.println(myCharValue1 + myCharValue2 + myCharValue3);
}
}
That is because chars refer to a number, which in turn has an ASCII representation.
Looking at an ASCII table you can see that the chars A, 2 and % have following values respectivly: 65, 50 and 37.
Adding those numbers together, you'll end up with 152 which is what you got in your example.
To print out those chars you could use following:
System.out.printf("%s%s%s&n", myCharValue1 + myCharValue2 + myCharValue3);
Which will print A2% (and a newline)
The concatenation + is for String. What you're doing is adding the numeric values of your chars and printing them together.
If you start with "" and then use + as Patrick Parker shows in his comment, it will become concatenation instead of simple addition and you'll get the result you expect.

System.in.read() is misbehaving when trying to enter integer value [duplicate]

This question already has answers here:
integer value read from System.in is not the value typed
(5 answers)
Closed 4 years ago.
I'm trying to enter integer value through System.in.read().
But when I'm reading the value it is giving different output 49 for 1, 50 for 2 & so on int e=(int)System.in.read();
System.out.print("\n"+e);
Because character 1 (i.e. char ch = '1') has ASCII code 49 (i.e. int code = '1' is 49).
System.out.println((int)'1'); // 49
To fix your examle, just substract code for 0:
int e = System.in.read() - '0';
System.out.println(e); // 1
You're reading char with function
System.in.read()
You can see the use of System.in.read() here. Also please check here how to read a value from user.
As other answers have mentioned, System.in.read() reads in a single character in the form of an int, or -1 if there is no input to read in. This means that characters read in with System.in.read() will be ints representing ASCII values of the read characters.
To read in an integer from System.in it might be easier to use a Scanner:
Scanner s = new Scanner(System.in);
int e = s.nextInt();
System.out.print("\n"+e);
s.close();
or, if you wish to stick to using System.in.read(), you can use Integer.parseInt(String) to obtain an integer off of the character input from System.in.read():
int e = Integer.parseInt("" + (char) System.in.read());
System.out.print("\n"+e);
Integer.parseInt(String) will throw a NumberFormatException you can catch if the input is not a number.

Categories