Open In App

Program to implement ASCII lookup table

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as ‘a’ or ‘@’ or an action of some sort. ASCII lookup table is a tabular representation of corresponding values associated to a character i.e. we can lookup the corresponding octal, decimal, hexadecimal or HTML ASCII of a character. 

Here, we are implementing an ASCII lookup table which takes a character as an input and returns the equivalent octal, decimal, hexadecimal and HTML ASCII value for the character. This ASCII lookup table works for alphabets, digits, operators, separators and special symbols. 

Example:

Input character = @ 
Output :
Octal value: 100
Decimal value: 64
Hexadecimal value: 40
HTML value: @
  • Step 1: Convert given character into it’s equivalent ASCII in decimal form. This can be done by implicitly typecasting the character into an integral value(or subtracting by null). 
  • Step 2: The value computed in step 1 becomes the decimal representation of the character. Convert the decimal value in octal and hexadecimal forms to obtain the ASCII of the input character into the given formats.
  • Step 3: Add characters &# as prefix and ; as postfix of the decimal ASCII, the expression obtained becomes the HTML ASCII of the given character. This way we can easily implement the ASCII lookup table. Follow the code below to see the implementation. 

Implementation:

C++




// C++ implementation of ASCII lookup table
#include <iostream>
#include <string>
using namespace std;
 
// Function to convert decimal value to
// equivalent octal value
int Octal(int decimal)
{
    int octal = 0;
    string temp = "";
    while (decimal > 0) {
        int remainder = decimal % 8;
        temp = to_string(remainder) + temp;
        decimal /= 8;
    }
 
    for (int i = 0; i < temp.length(); i++)
        octal = (octal * 10) + (temp[i] - '0');
 
    return octal;
}
 
// Function to convert decimal value to
// equivalent hexadecimal value
string Hexadecimal(int decimal)
{
    string hex = "";
    while (decimal > 0) {
 
        int remainder = decimal % 16;
        if (remainder >= 0 && remainder <= 9)
            hex = to_string(remainder) + hex;
        else
            hex = (char)('A' + remainder % 10) + hex;
        decimal /= 16;
    }
    return hex;
}
 
// Function to convert decimal value to
// equivalent HTML value
string HTML(int decimal)
{
    string html = to_string(decimal);
    html = "&#" + html + ";";
    return html;
}
 
// ASCII lookup table
void ASCIIlookuptable(char ch)
{
    // Implicit typecasting converts the
    // character into it's equivalent ASCII
    int decimal = ch;
 
    cout << "Octal value: " << Octal(decimal) << endl;
    cout << "Decimal value: " << decimal << endl;
    cout << "Hexadecimal value: " << Hexadecimal(decimal) << endl;
    cout << "HTML value: " << HTML(decimal);
}
 
// Driver function
int main()
{
    char ch = '@';
    ASCIIlookuptable(ch);
    return 0;
}


Java




// Java implementation for ASCII table lookup
import java.util.*;
import java.lang.*;
 
class GeeksforGeeks {
 
    // Function to convert decimal value to
    // equivalent octal value
    static int Octal(int decimal)
    {
        int octal = 0;
        String temp = "";
        while (decimal > 0) {
            int remainder = decimal % 8;
            temp = remainder + temp;
            decimal /= 8;
        }
 
        for (int i = 0; i < temp.length(); i++)
            octal = (octal * 10) + (temp.charAt(i) - '0');
         
        return octal;
    }
 
    // Function to convert decimal value to
    // equivalent hexadecimal value
    static String Hexadecimal(int decimal)
    {
        String hex = "";
        while (decimal > 0) {
 
            int remainder = decimal % 16;
            if (remainder >= 0 && remainder <= 9)
                hex = remainder + hex;
            else
                hex = (char)('A' + remainder % 10) + hex;
            decimal /= 16;
        }
        return hex;
    }
 
    // Function to convert decimal value to
    // equivalent HTML value
    static String HTML(int decimal)
    {
        String html = "";
        html = html + decimal;
        html = "&#" + html + ";";
        return html;
    }
 
    // ASCII lookup table
    static void ASCIIlookuptable(char ch)
    {
        // Implicit typecasting converts the
        // character into it's equivalent ASCII
        int decimal = ch;
 
        System.out.println("Octal value: " + Octal(decimal));
        System.out.println("Decimal value: " + decimal);
        System.out.println("Hexadecimal value: " + Hexadecimal(decimal));
        System.out.println("HTML value: " + HTML(decimal));
    }
 
    // driver function
    public static void main(String args[])
    {
        char ch = '@';
        ASCIIlookuptable(ch);
    }
}


Python3




# Function to convert decimal value to equivalent octal value
def Octal(decimal):
    octal = 0
    temp = ""
    while decimal > 0:
        remainder = decimal % 8
        temp = str(remainder) + temp
        decimal //= 8
 
    for i in range(len(temp)):
        octal = (octal * 10) + int(temp[i])
 
    return octal
 
# Function to convert decimal value to equivalent hexadecimal value
def Hexadecimal(decimal):
    hex = ""
    while decimal > 0:
        remainder = decimal % 16
        if remainder >= 0 and remainder <= 9:
            hex = str(remainder) + hex
        else:
            hex = chr(ord('A') + remainder % 10) + hex
        decimal //= 16
    return hex
 
# Function to convert decimal value to equivalent HTML value
def HTML(decimal):
    html = str(decimal)
    html = "&#" + html + ";"
    return html
 
# ASCII lookup table
def ASCIIlookuptable(ch):
    # Implicit typecasting converts the character into its equivalent ASCII
    decimal = ord(ch)
 
    print("Octal value: ", Octal(decimal))
    print("Decimal value: ", decimal)
    print("Hexadecimal value: ", Hexadecimal(decimal))
    print("HTML value: ", HTML(decimal))
 
# Driver function
def main():
    ch = '@'
    ASCIIlookuptable(ch)
 
if __name__ == '__main__':
    main()


C#




// C# implementation for ASCII
// table lookup
using System;
 
class GeeksforGeeks {
 
    // Function to convert decimal value to
    // equivalent octal value
    static int Octal(int decima)
    {
        int octal = 0;
        String temp = "";
        while (decima > 0)
        {
            int remainder = decima % 8;
            temp = remainder + temp;
            decima /= 8;
        }
 
        for (int i = 0; i < temp.Length; i++)
            octal = (octal * 10) +
                    (temp[i] - '0');
         
        return octal;
    }
 
    // Function to convert decimal value
    //  to equivalent hexadecimal value
    static String Hexadecimal(int decima)
    {
        String hex = "";
        while (decima > 0)
        {
 
            int remainder = decima % 16;
            if (remainder >= 0 &&
                remainder <= 9)
                hex = remainder + hex;
            else
                hex = (char)('A' + remainder %
                                    10) + hex;
            decima /= 16;
        }
        return hex;
    }
 
    // Function to convert decimal
    // value to equivalent HTML value
    static String HTML(int decima)
    {
        String html = "";
        html = html + decima;
        html = "&#" + html + ";";
        return html;
    }
 
    // ASCII lookup table
    static void ASCIIlookuptable(char ch)
    {
         
        // Implicit typecasting converts the
        // character into it's equivalent ASCII
        int decima = ch;
 
        Console.WriteLine("Octal value: " +
                           Octal(decima));
        Console.WriteLine("Decimal value: " +
                           decima);
        Console.WriteLine("Hexadecimal value: " +
                           Hexadecimal(decima));
        Console.Write("HTML value: " +
                       HTML(decima));
    }
 
    // Driver Code
    public static void Main()
    {
        char ch = '@';
        ASCIIlookuptable(ch);
    }
}
 
// This code is contributed by nitin mittal.


Javascript




// JavaScript code implementation
// JavaScript implementation of ASCII lookup table
    function Octal(decimal) {
    let octal = 0;
    let temp = "";
        while (decimal > 0) {
        let remainder = decimal % 8;
        temp = remainder.toString() + temp;
        decimal = Math.floor(decimal / 8);
    }
 
        for (let i = 0; i < temp.length; i++)
        octal = octal * 10 + Number(temp.charAt(i));
 
    return octal;
    }
 
function Hexadecimal(decimal) {
let hex = "";
    while (decimal > 0) {
    let remainder = decimal % 16;
        if (remainder >= 0 && remainder <= 9)
            hex = remainder.toString() + hex;
        else
            hex = String.fromCharCode(65 + remainder % 10) + hex;
        decimal = Math.floor(decimal / 16);
    }
    return hex;
}
 
function HTML(decimal) {
let html = decimal.toString();
    html = "&#"+ html + ";";
    return html;
}
 
function ASCIIlookuptable(ch) {
let decimal = ch.charCodeAt(0);
    console.log("Octal value: " + Octal(decimal));
    console.log("Decimal value: " + decimal);
    console.log("Hexadecimal value: " + Hexadecimal(decimal));
    console.log("HTML value: " + HTML(decimal));
}
 
// Drive code function
let ch = "@";
ASCIIlookuptable(ch);


Output

Octal value: 100
Decimal value: 64
Hexadecimal value: 40
HTML value: @

Time Complexity: O(log8(Decimal)), as we are using a loop and in each traversal we are decrementing N by floor division of 8.
Auxiliary Space: O(log8(Decimal)), as we are using extra space for the answer.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads