How to get ASCII representation of a character? - java

I have very basic question.
How is possible for int a = 'a' to give 97 in output.
Below is my code:
class myClass {
int last = 'a' ;
myClass () {
System.out.println(last );
}
}

You can have a look at this: Why are we allowed to assign char to a int in java?
Basically, you are assigning a char to your int. A char is technically an unsigned 16-bit character. That's why you can assign it to an int.
Hope this helps.

You can basically cast the char to the int and store it as int:
int a = (int)'a';
System.out.println(a); //prints 97
Since Java can do the basic castings from your type specifications, you do not need to explicity write casting even.
int a = 'a';
System.out.println(a); // prints 97

The output is the ASCII value of the character stored in last. The ASCII value of the character 'a' is 97, and hence the output on the console is 97.

you have to take 'a' as a char
char char1 = 'a';
then cast it to int
int num = (int) char1 ;

Related

Integer arithmetic with character

I have a simple block of code, can someone explain to me why this acceptable in Java?
int a = 10;
int c = 'A' + (a -1);
System.out.println(c);
The result of this displayed in compiler is: 74.
So where exactly the values of seventies come from?
Thanks for your answers.
In Java a char can be (explicitly or implicitly) casted to int, it then uses the ASCII value associated to this character.
It your case, the seventies comes from the character 'A'. The ASCII value of this character is 65. So the system implicitly does the casting 'A' → 65. Your calculation does:
c = 'A' + (a-1)
↓
c = 65 + (10-1)
↓
c = 74
The ascii value of 'A' is 65. Check this link for complete reference. The conversion occurs through (implicit) widening the char datatype to int datatype using its unicode value, which in this case for 'A' is 65 (jls 5.6.1).
Char is implicitly converted into integer resulting 'A' = 65
so 65+9 = 74
When you assign value of one data type to another, the two types might not be compatible with each other. It should be casted to correct type:
There are 2 types:-
Widening or Automatic Type Conversion.
which casts in this sequence:
byte -> short -> int -> long -> float -> double
char -> int
example :
int i = 100; //i is 100
float f = i; // f is 100.0
Narrowing or Explicit
Conversion. which casts in this sequence:
double -> float -> long -> int ->short -> byte
example:
double d = 100.100; //d is 100.100
int i = (int)d; //i is 100
So, When a Character value is used in integer context. It is automatically casted to int.
In your case:
int a = 10;
int c = 'A' + (a -1); //c = 65 + (10-1)
'A' ascii value is 65, thus you get c = 74
Hope this helps.

Cannot comprehend Output Of The Program

import java.util.Scanner;
public class asdf
{
public static void main(){
String temp = "165";
int ch = temp.charAt(0);
int ch1 = temp.charAt(1);
int ch2 = temp.charAt(2);
System.out.println(ch);
System.out.println(ch1);
System.out.println(ch2);
}
}
Output:
49
54
53
I cannot understand the output.Is it implicit cast according to which a character is converted to a number . Any help is appreciated.
It is printing the ASCII values of the digits. 1,6,5.
Note that the ASCII value of 0 is 48. So, 49 is ASCII value of character 1 and so on.
Have a look here: http://www.asciitable.com/
Try this to get the actual characters printed instead of the ASCII values:
char ch = temp.charAt(0);
char ch1 = temp.charAt(1);
char ch2 = temp.charAt(2);
char is a subset of int. It contains values from 0 to 2^16-1. Each value corresponds with a character.
When you assign a char to an int variable and print that variable, you'll see that int value that corresponds with the character.
'1' corresponds with 49
'6' corresponds with 54
'5' corresponds with 53
it gives the ASCII value, you can cast it as char to get the expected answer
System.out.println((char) ch);
System.out.println((char) ch1);
System.out.println((char) ch2);

How to turn a char(0-9) into int in java?

char c = '7';
int i = c - '0';
// Here value of '0' is 48 and c = '7' is 55. Therefore, i = 55 - 48 or 7;
More example:
String s = "453467"
Here, you may need the integer value located at 3rd cell.
You can first make a char variable. Then convert the char variable into an int.
char c = s.charAt(3);
int i = c - '0';
Sometimes this thing is needed for solving programming problems and to take the input from the user as an string.
Can you tell me the other ways to convert them. This will be very helpful for me to solve problems. THANKS!!
You want:
Character.getNumericValue(char ch)
Use the Integer class:
`int yourInt = Integer.parseInt(Character.toString('0'));

convert integer to a character

I am having a problem with my java code, basically I want to do this;
char letter = 'a';
char convertedletter = letter + 5;
System.out.print(convertedletter);
the output should be f, but the output I get is 108. How could I make it an f?
try this:
char letter = 'a';
char convertedletter;
convertedletter = (char) (letter + 5);
System.out.print(convertedletter);
The operator "+" is used to add numbers or concatenate String, since you are using it whit a char (only one character) and a int it would return a int unless you converted it to char with is what you want.
try this:
char convertedletter = (char) (letter + 5);
You should have cast while adding. As 5 is integer, convertedletter get converted to Integer and hence you get output as 108 as ascii value of 'a' is 103.
Use should have
char convertedletter = (char) (letter + 5);
Also read this, this for more info.

Why doesn't my compare work between char and int in Java?

char c = '0';
int i = 0;
System.out.println(c == i);
Why does this always returns false?
Although this question is very unclear, I am pretty sure the poster wants to know why this prints false:
char c = '0';
int i = 0;
System.out.println(c == i);
The answer is because every printable character is assigned a unique code number, and that's the value that a char has when treated as an int. The code number for the character 0 is decimal 48, and obviously 48 is not equal to 0.
Why aren't the character codes for the digits equal to the digits themselves? Mostly because the first few codes, especially 0, are too special to be used for such a mundane purpose.
The char c = '0' has the ascii code 48. This number is compared to s, not '0'. If you want to compare c with s you can either do:
if(c == s) // compare ascii code of c with s
This will be true if c = '0' and s = 48.
or
if(c == s + '0') // compare the digit represented by c
// with the digit represented by s
This will be true if c = '0' and s = 0.
The char and int value can not we directly compare we need to apply casting. So need to casting char to string and after string will pars into integer
char c='0';
int i=0;
Answer is like
String c = String.valueOf(c);
System.out.println(Integer.parseInt(c) == i)
It will return true;
Hope it will help you
Thanks
You're saying that s is an Integer and c (from what I see) is a Char.. so there you, that's the problem: Integer vs. Char comparation.

Categories

Resources