The Wayback Machine - https://web.archive.org/web/20240602101742/https://www.geeksforgeeks.org/program-print-ascii-value-character/
Open In App

Program to print ASCII Value of a character

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

Given a character, we need to print its ASCII value in C/C++/Java/Python.

Examples :

Input : a 
Output : 97

Input : D
Output : 68

Here are few methods in different programming languages to print ASCII value of a given character : 

  1. Python code using ord function : ord() : It converts the given string of length one, returns an integer representing the Unicode code point of the character. For example, ord(‘a’) returns the integer 97. 

Python




# Python program to print
# ASCII Value of Character
 
# In c we can assign different
# characters of which we want ASCII value
 
c = 'g'
# print the ASCII value of assigned character in c
print("The ASCII value of '" + c + "' is", ord(c))


Output

("The ASCII value of 'g' is", 103)

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

  1. C code: We use format specifier here to give numeric value of character. Here %d is used to convert character to its ASCII value. 

C




// C program to print
// ASCII Value of Character
#include <stdio.h>
int main()
{
    char c = 'k';
 
    // %d displays the integer value of a character
    // %c displays the actual character
    printf("The ASCII value of %c is %d", c, c);
    return 0;
}


Output

The ASCII value of k is 107

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

  1. C++ code: Here int() is used to convert a character to its ASCII value. 

CPP




// CPP program to print
// ASCII Value of Character
#include <iostream>
using namespace std;
int main()
{
    char c = 'A';
    cout << "The ASCII value of " << c << " is " << int(c);
    return 0;
}


Output

The ASCII value of A is 65

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

  1. Java code : Here, to find the ASCII value of c, we just assign c to an int variable ascii. Internally, Java converts the character value to an ASCII value. 

Java




// Java program to print
// ASCII Value of Character
public class AsciiValue {
 
    public static void main(String[] args)
    {
 
        char c = 'e';
        int ascii = c;
        System.out.println("The ASCII value of " + c + " is: " + ascii);
    }
}


Output

The ASCII value of e is: 101

Time Complexity: O(1) // since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant

  1. C# code : Here, to find the ASCII value of c, we just assign c to an int variable ascii. Internally, C# converts the character value to an ASCII value. 

CSHARP




// C# program to print
// ASCII Value of Character
using System;
 
public class AsciiValue
{
    public static void Main()
    {
 
        char c = 'e';
        int ascii = c;
        Console.Write("The ASCII value of " +
                        c + " is: " + ascii);
    }
}
 
// This code is contributed
// by nitin mittal


Output

The ASCII value of e is: 101

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

  1. JavaScript code: Here, to find the ASCII value of the character, the charCodeAt() method is used to get the ASCII value of the character at a specific index in a string.

Javascript




// JavaScript program to print
// ASCII Value of Character
 
console.log("The ASCII value of " + 'A' + " is " + 'A'.charCodeAt(0));
 
// This Code is Contributed by Prasad Kandekar(prasad264)


Output:

The ASCII value of A is 65

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

Here is a method to print the ASCII value of the characters in a string using python:

Python3




print("Enter a String: ", end="")
text = input()
textlength = len(text)
for char in text:
    ascii = ord(char)
    print(char, "\t", ascii)


Input:

Enter a String: Aditya Trivedi

Output:

A 65
d 100
i 105
t 116
y 121
a 97
32
T 84
r 114
i 105
v 118
e 101
d 100
i 105

Time Complexity: O(N), where N is the length of the input string.
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

C++ Program To Print ASCII Value of a Character
Given a character, we need to print its ASCII value in C++. Examples : Input: a Output: 97 Input: D Output: 68 C++ code: Here int() is used to convert character to its ASCII value. C/C++ Code // C++ program to print // ASCII Value of Character #include &lt;iostream&gt; using namespace std; // Driver code int main() { char c = 'A'; cout &lt;&lt;
1 min read
Replace every character of string by character whose ASCII value is K times more than it
Given string str consisting of lowercase letters only and an integer k, the task is to replace every character of the given string with a character whose ASCII value is k times more than it. If the ASCII value exceeds 'z', then start checking from 'a in a cyclic manner. Examples: Input: str = "abc", k = 2 Output: cde Explanation: a is moved by 2 ti
9 min read
Check if a string contains a non-ASCII value in Julia - ascii() Method
The ascii() is an inbuilt function in Julia which is used to convert a specified string to a String type and also check the presence of ASCII data. If the non-ASCII byte is present, throw an ArgumentError indicating the position of the first non-ASCII byte. Syntax: ascii(s::AbstractString) Parameters:s::AbstractString: Specified stringReturns: It r
1 min read
Convert Character value to ASCII value in R Programming - charToRaw() Function
charToRaw() function in R Language is used to convert the given character to their corresponding ASCII value or "raw" objects. Syntax: charToRaw(x)Parameters: x: Given characters to be converted Example 1: C/C++ Code # R program to illustrate # charToRaw function # Calling charToRaw() function over # some alphabets x &lt;- charToRaw(&quot;a&quot;)
1 min read
What is ASCII - A Complete Guide to Generating ASCII Code
The American Standard Code for Information Interchange, or ASCII, is a character encoding standard that has been a foundational element in computing for decades. It plays a crucial role in representing text and control characters in digital form. Historical BackgroundASCII has a rich history, dating back to its development in the early 1960s. Origi
13 min read
Substring with maximum ASCII sum when some ASCII values are redefined
Given a string W, and two arrays X[] and B[] of size N each where the ASCII value of character X[i] is redefined to B[i]. Find the substring with the maximum sum of the ASCII (American Standard Code for Information Interchange) value of the characters. Note: Uppercase &amp; lowercase both will be present in the string W. Input: W = "abcde", N = 1,
9 min read
ASCII NULL, ASCII 0 ('0') and Numeric literal 0
The ASCII (American Standard Code for Information Interchange) NULL and zero are represented as 0x00 and 0x30 respectively. An ASCII NULL character serves as a sentinel character of strings in C/C++. When the programmer uses '0' in his code, it will be represented as 0x30 in hex form. What will be filled in the binary integer representation in the
3 min read
Python program to print words from a sentence with highest and lowest ASCII value of characters
Given a string S of length N, representing a sentence, the task is to print the words with the highest and lowest average of ASCII values of characters. Examples: Input: S = "every moment is fresh beginning"Output:Word with minimum average ASCII values is "beginning".Word with maximum average ASCII values is "every".Explanation:The average of ASCII
5 min read
Program to print ASCII Value of all digits of a given number
Given an integer N, the task is to print the ASCII value of all digits of N. Examples: Input: N = 8Output: 8 (56)Explanation:ASCII value of 8 is 56 Input: N = 240Output:2 (50)4 (52)0 (48) Approach: Using the ASCII table shown below, the ASCII value of all the digits of N can be printed: DigitASCII Value048149250351452553654755856957It can be observ
5 min read
ASCII Value of a Character in C
American Standard Code for Information Interchange (ASCII) is a character encoding standard that assigns a unique numerical value to all characters including special symbols. In C programming, the ASCII value of the character is stored instead of the character itself. For example, the ASCII value of 'A' is 65. Each character or special character is
2 min read