Program to implement ASCII lookup table

22 Jun, 2022
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: &#064;
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. 
C++
Java
C#
// C++ implementation of ASCII lookup table#include <iostream>#include <string>using namespace std; // Function to convert decimal value to// equivalent octal valueint 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 valuestring 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 valuestring HTML(int decimal){    string html = to_string(decimal);    html = "&#" + html + ";";    return html;} // ASCII lookup tablevoid 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 functionint main(){    char ch = '@';    ASCIIlookuptable(ch);    return 0;}
// Java implementation for ASCII table lookupimport 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);    }}
// C# implementation for ASCII// table lookupusing 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.
Output:
Octal value: 100
Decimal value: 64
Hexadecimal value: 40
HTML value: &#064;
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.
nitin mittal
rohitsingh57
ASCII
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.