Skip to main content

← Back to Overview

ASCII Value of Character

Concept Explanation

What is it?

The ASCII (American Standard Code for Information Interchange) value is a numerical representation of characters. Each character, including letters, numbers, symbols, and control characters, is assigned a unique integer value from 0 to 127. This program takes a character as input and displays its corresponding ASCII integer value.

Why is it important?

ASCII encoding is fundamental to how computers store and process text. Understanding ASCII values is crucial for:

  • Character manipulation and comparisons.
  • Basic text processing and parsing.
  • Understanding character sets and encodings.
  • Low-level programming where character representation matters.

Where is it used?

  • Text Editors: Storing and displaying characters.
  • Networking: Transmitting text data across networks.
  • Data Storage: Saving text files.
  • Security: In cryptography, characters might be manipulated based on their numerical values.
  • Input Validation: Checking if a character falls within a certain range (e.g., all uppercase letters).

Real-world example

When you type a letter 'A' on your keyboard, the computer doesn't store 'A' directly. Instead, it converts 'A' into its ASCII value (65) and stores that number in memory. When the computer needs to display 'A' on the screen, it looks up 65 in the ASCII table and renders the character 'A'.


Algorithm

  1. Start.
  2. Get a single character (ch) from the user.
  3. Obtain the ASCII integer value corresponding to ch.
  4. Display the original character and its ASCII value.
  5. End.

Edge Cases:

  • Inputting more than one character (handled by taking only the first character or explicit error).
  • Non-printable ASCII characters: These will have ASCII values but might not render visibly.
  • Extended ASCII or Unicode characters: Standard ASCII only covers 0-127. Other encodings (like Unicode) use larger ranges. This program focuses on standard ASCII.

Implementations

import java.util.Scanner;

public class ASCIIValue {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a character: ");
char character = scanner.next().charAt(0); // Read the first character of the input

// The ASCII value is implicitly converted when cast to an int
int asciiValue = (int) character;

System.out.println("The character is: " + character);
System.out.println("The ASCII value is: " + asciiValue);

scanner.close();
}
}

Explanation

  • Java: When a char type is cast to an int, its corresponding ASCII/Unicode value is obtained. Scanner is used for input.
  • Python: The built-in ord() function directly returns the Unicode code point of a single character, which for ASCII characters is the same as its ASCII value.
  • C: A char in C is an integral type. When printed with %d, its integer ASCII value is displayed. scanf("%c", ...) reads a single character.
  • Oracle: The ASCII() SQL function (usable in PL/SQL) returns the ASCII value of the first character of a string.

Complexity Analysis

  • Time Complexity: O(1) - The operation is a direct lookup or conversion.
  • Space Complexity: O(1) - A fixed number of variables are used.

Flowchart

Sample Dry Run

StepCharacterASCII ValueDescription
Input'A'-User enters 'A'
Process'A'65'A' is converted to its ASCII value
Output-65Display "The character 'A' is 65"
End--Program terminates

Practice Problems

Easy

  • Modify the program to convert an ASCII value back to a character.
  • Display the ASCII values for all characters from 'A' to 'Z'.

Medium

  • Implement a simple Caesar cipher (encryption) that shifts alphabetic characters based on their ASCII values.
  • Write a program that takes a string and converts each character to its ASCII value.

Hard

  • Explore different character encodings (e.g., UTF-8) and how characters are represented in memory beyond basic ASCII.

"The function of a good software is to make the complex appear simple." - Grady Booch

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir