0 votes
1 view
in Java by (1.3k points)

I have String name = "admin";
then I do String char = name.substring(0,1); //char="a"

I want to convert the char to its ASCII value (97), how can I do this in java?

1 Answer

0 votes
by (13.2k points)

The character itself gets converted into the ascii code, once we make the character an integer.

You can either cast it,

String name = "admin";

String char = name.substring(0,1); //char="a"

int ascii = (int) char;

OR 

You can directly convert the character to int

String name = "admin";

String char = name.substring(0,1); //char="a"

int ascii = char;

Related questions

0 votes
1 answer
asked Jul 9, 2019 in Java by Nigam (4.1k points)
0 votes
1 answer
asked Jul 9, 2019 in Java by Aditya98 (1.3k points)
0 votes
2 answers
asked Jul 9, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...