VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-convert-unicode-to-ascii/

⇱ Program to Convert Unicode to ASCII - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to Convert Unicode to ASCII

Last Updated : 23 Jul, 2025

Given a Unicode number, the task is to convert this into an ASCII (American Standard Code for Information Interchange) number.

ASCII is a character encoding standard used in communication systems and computers. It uses 7-bit encoding to encode 128 different characters 0-127. These values generally include numbers, letters, punctuation, and some control characters. It primarily focuses on representing English symbols.

Unicode is a character encoding well known used in conversation structures and computer systems. Unlike ASCII, which uses 7-bit encoding and encodes 128 unique characters (zero-127), Unicode makes use of variable-period encoding to symbolize a full-size variety of characters from numerous scripts and languages. Unicode can represent over 1,000,000 distinct characters, making it a comprehensive character encoding standard.

Examples:

Input: Unicode = 'A'
Output: ASCII = 65

Input: Unicode = 'Z'
Output: ASCII = 90

Approach: Follow the below steps to convert unicode to ASCII number

  • Declare a string variable unicodeInput and initialize it with the Unicode character "A".
  • Call the unicodeToAscii function with unicodeInput as an argument.
  • Define the unicodeToAscii function that takes a string unicodeNum as a parameter.
  • In the try block:
    • Extract the first character from the unicodeNum string.
    • Convert the Unicode character to ASCII by casting it to an integer.
    • Return the ASCII value.
  • In the catch block:
    • Handle StringIndexOutOfBoundsException by printing an error message with the exception's message.
    • Return -1 to indicate an error.
  • In the main function:
    • Check if the result from unicodeToAscii is not equal to -1.
    • If true, print the Unicode input and the corresponding ASCII output.
  • The program outputs the Unicode character "A" and its corresponding ASCII value.

Following is the code to get the ASCII value of a given character.


Output
Unicode: A
ASCII: 65

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: