Convert Character to ASCII Numeric Value in Java

Convert chartacter to ascii in java

In this post, we will see How to convert Character to ASCII Numeric Value in Java.
There are multiple ways to convert Character to ASCII Numeric Value in Java

By casting char to int

You can simply get char from String using charAt() and cast it to int.
Here is an example:

Output:

Ascii value of e is: 101

You can even directly assign char to int, but it is good idea to explicitly cast it for readabiliy.
You can change highlighted code to below line and program will still work:

Using toCharArray()

You can simply use index with toCharArray() to get ASCII value of character in the String.
Here is an example:

Output:

Ascii value of e is: 101

Using String’s getBytes()

You can convert String to byte array using getBytes(StandardCharsets.US_ASCII) and this byte array will contain character’s ASCII values. You can access individual value by accessing byte array by index.
Here is an example:

Output:

Ascii value of e is: 101ASCII values for all characters are:72 101 108 108 111

Using String’s char() [Java 9+]

You can convert String to IntStream using String’s chars() method, use boxed() to convert it to Stream of wrapper type Integer and collect to the list. Result list will contain all the ascii value of the characters and you can use index to access individual ASCII value of character.
Here is an example:

Output:

ASCII values for all characters are:72 101 108 108 111

Convert a String of letters to an int of corresponding ascii

If you want to convert entire String into concatenated ASCII value of int type, you can create StringBuilder from String’s ASCII values and convert it to BigInteger.
Here is an example:

Output:

72101108108111

That’s all about Convert Character to ASCII in Java

Related Posts

  • 30 September

    How to compare characters in Java

    Learn about how to compare characters in java using different methods.

  • 17 March

    New line character in java

    Table of ContentsUsing \n or \r\nUsing Platform independent line breaks (Recommended) In this post, we will see about new line character in java and how to add new line character to a String in different operating systems. Operating systems have different characters to denote the end of the line. Linux and new mac: In Linux, […]

  • 09 October

    Java program to print vowels in a String

    Table of ContentsPrint Vowels in a StringCount number of Vowels in the String In this post, we will see how to print and count vowels in a string. Print Vowels in a String If any character in String satisfy below condition then it is vowel and we will add it to Hashset. character==’a’ || character==’A’ […]

  • 06 October

    Java remove last character from string

    Learn about how to remove last character from String in java using different ways.

  • 06 October

    Convert char to lowercase java

    Table of ContentsMethod signatureParametersReturn type You can use Character class’s toLowerCase method to convert char to lowercase in java. Method signature [crayon-616d2baa8be2e601682402/] Parameters ch is primitive character type. Return type return type is char. If char is already lowercase then it will return same. [crayon-616d2baa8be35238418355/] When you run above program, you will get below output: […]

  • 06 October

    Convert char to uppercase java

    Table of ContentsMethod signatureParametersReturn type You can use Charater class’s touppercase method to convert char to uppercase in java. Method signature [crayon-616d2baa8c6dc558574121/] Parameters ch is primitive character type. Return type return type is char. If char is already uppercase then it will return same. [crayon-616d2baa8c6e3879230205/] When you run above program, you will get below output: […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.