Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 Dec 10, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Jul 9, 2019 in Java by Nigam (4k 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)

Browse Categories

...