Converting a number to ASCII in programming languages allows you to work with character data rather than just numeric values. Each ASCII character has a corresponding numeric value that can be used to represent that character. For example, the ASCII value for uppercase ‘A’ is 65. By converting a number to its corresponding ASCII character, you can print letters and symbols rather than just numbers.
There are a few simple ways to convert a number to its ASCII character equivalent in languages like C, Python, Java, and others. In this comprehensive guide, we’ll cover:
What is ASCII?
ASCII (American Standard Code for Information Interchange) is a character encoding standard that uses numeric values to represent characters. It was developed in the 1960s and is based on the English alphabet. ASCII defines 128 different characters that can be used for displaying text.
Each ASCII character has a corresponding numeric value between 0 and 127. For example:
Character | ASCII Value |
---|---|
A | 65 |
B | 66 |
C | 67 |
By converting the numeric value back to its character, you can represent text and symbols using numbers.
Why Convert Numbers to ASCII?
There are a few key reasons why you may need to convert a numeric value to its corresponding ASCII character:
- To display readable text using numeric data
- To print out text characters and symbols in a program
- To convert input to a readable string
- To manipulate and process character data
- To store text data using numeric values
For example, you may want to take a user’s input as a number but display it back as a character, or store character data in a numeric format in memory or a file.
How to Convert to ASCII in C
In C, you can convert an integer to its corresponding ASCII character using the printf()
function and %c
format specifier.
For example:
int main() {
int number = 65;
printf("%c", number);
return 0;
}
This would print “A” to the console since 65 is the ASCII value for uppercase A.
You can also typecast the number to a char to convert it:
int main() {
int number = 65;
char letter = (char)number;
printf("%c", letter);
return 0;
}
This typecasts the int
variable to a char
and prints the letter “A”.
C Example Program
Here is a full C program that converts a number to ASCII:
#include <stdio.h>
int main() {
int number = 65;
printf("ASCII value is: %d\n", number);
char letter = (char)number;
printf("Character is: %c\n", letter);
return 0;
}
This prints:
ASCII value is: 65 Character is: A
We first print the ASCII decimal value 65, then typecast it to a char
and print the resulting character A.
How to Convert to ASCII in Python
In Python, you can convert an integer to ASCII using the chr()
built-in function.
For example:
number = 65
print(chr(number))
The chr()
function takes the integer value 65 and converts it to the ASCII character “A”.
You can also store the returned character:
number = 65
letter = chr(number)
print(letter)
This stores the ASCII character in the letter
variable, then prints it.
Python Example Program
number = 65
print("ASCII value:", number)
letter = chr(number)
print("Character:", letter)
This would print:
ASCII value: 65 Character: A
Demonstrating how to convert the number 65 to its ASCII character representation.
How to Convert to ASCII in Java
In Java, you can convert an int
to its ASCII character by casting it to a char
.
For example:
int number = 65;
char letter = (char) number;
System.out.println(letter);
This casts the int
65 to a char
which prints “A”.
You can also use other utility methods like:
int number = 65;
char letter = Character.forDigit(number, 10);
System.out.println(letter);
This uses the Character.forDigit()
method to convert the number 65 to its ASCII representation.
Java Example Program
public class Main {
public static void main(String[] args) {
int number = 65;
System.out.println("ASCII value: " + number);
char letter = (char)number;
System.out.println("Character: " + letter);
}
}
When run, this prints:
ASCII value: 65 Character: A
Demonstrating the conversion from an integer to its ASCII character in Java.
How to Convert to ASCII in JavaScript
In JavaScript, you can use the String.fromCharCode()
method to convert a number to ASCII.
For example:
let number = 65;
let letter = String.fromCharCode(number);
console.log(letter);
This converts the number 65 to its ASCII character and prints “A”.
Alternatively, you can create a String
from the number:
let number = 65;
let letter = String.fromCodePoint(number);
console.log(letter);
The fromCodePoint()
static method also returns the ASCII character.
JavaScript Example
let number = 65;
console.log("ASCII value: " + number);
let letter = String.fromCharCode(number);
console.log("Character: " + letter);
This prints:
ASCII value: 65 Character: A
Demonstrating the conversion in JavaScript.
How to Convert ASCII to Number
You can also convert in the opposite direction – from an ASCII character to its numeric value. This allows you to work with character data as numeric values.
For example, in Python:
letter = 'A'
number = ord(letter)
print(number)
The ord()
built-in function takes a character and returns its ASCII decimal value. This prints “65”.
Similarly, in C you can use typecasting:
char letter = 'A';
int number = (int) letter;
printf("%d", number);
Casting the char
to an int
prints “65”.
And in Java:
char letter = 'A';
int number = (int) letter;
System.out.println(number);
So converting a character to its ASCII numeric value is similar to converting a number to a character.
Table of ASCII Values
Here is a reference table showing some common ASCII characters and their numeric values:
Character | ASCII Value |
---|---|
A | 65 |
B | 66 |
C | 67 |
0 | 48 |
1 | 49 |
$ | 36 |
! | 33 |
You can reference this table when working with ASCII values in code.
Conclusion
Converting between numeric ASCII values and character data is easy in most programming languages. By using the built-in functions and typecasting features of each language, you can work with textual data in numeric form and vice versa.
Knowing how to convert between numbers and ASCII characters enables you to:
- Display and print text using numeric data
- Store and manipulate character strings as numbers
- Pass character input as numeric values in a program
- Work with textual content as numeric data
So whether you need to take numeric input and convert it to a string, or store character data in a numeric array, converting between ASCII values and characters is a useful skill.