Skip to 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();
    }
}
# Get a character from user
char_input = input("Enter a character: ")

# Ensure only one character is processed
if len(char_input) == 1:
    character = char_input[0]
    ascii_value = ord(character) # ord() returns the ASCII/Unicode value
    print(f"The character is: '{character}'")
    print(f"The ASCII value is: {ascii_value}")
else:
    print("Please enter a single character.")
#include <stdio.h>

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch); // Reads a single character

    // %d displays the integer value of a character
    // %c displays the actual character
    printf("The ASCII value of '%c' is %d", ch, ch); // Added

    return 0;
}
SET SERVEROUTPUT ON;
DECLARE
  input_char  CHAR(1) := '&Enter_a_Character';
  ascii_val   NUMBER;
BEGIN
  DBMS_OUTPUT.PUT_LINE('--- ASCII Value of Character ---');

  ascii_val := ASCII(input_char); -- ASCII() function returns the ASCII value

  DBMS_OUTPUT.PUT_LINE('The character is: ' || input_char);
  DBMS_OUTPUT.PUT_LINE('The ASCII value is: ' || ascii_val);
END;
/

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

graph TD
    A[Start] --> B[Get Character]
    B --> C[Convert Character to ASCII Value]
    C --> D[Display Character and ASCII Value]
    D --> E[End]

Sample Dry Run

Step Character ASCII Value Description
Input 'A' - User enters 'A'
Process 'A' 65 'A' is converted to its ASCII value
Output - 65 Display "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