Menu
In C programming, a character variable holds ASCII value (an integer number between 0 an 127) rather than character itself. You will learn how to find ASCII value of a character in this program. Jul 11, 2012 Decimal to ASCII char. Is there any way to convert an int value to a char value that is corresponding to its ASCII value? Int a a = 87 ASCII of a = 'W' Thanks. You can simply cast it. 1 2: int a = 87; char W = staticcast(a); vlad from moscow. You need not fo any conversion because int and char are integral.
For example, if the integer was 97, the character would be 'a', or 98 to 'b'.
Ciro Santilli 新疆改造中心996ICU六四事件158k3535 gold badges595595 silver badges499499 bronze badges
Derpster13Derpster13
5 Answers
In C,
int
, char
, long
, etc. are all integers.They typically have different memory sizes and thus different ranges as in
INT_MIN
to INT_MAX
. char
and arrays of char
are often used to store characters and strings. Integers are stored in many types: int
being the most popular for a balance of speed, size and range.ASCII is by far the most popular character encoding, but others exist. The ASCII code for an 'A' is 65, 'a' is 97, 'n' is 10, etc. ASCII data is most often stored in a
char
variable. If the C environment is using ASCII encoding, the following all store the same value into the integer variable.To convert an
int
to a char
, simple assign:This warning comes up because
int
typically has a greater range than char
and so some loss-of-information may occur. By using the cast (char)
, the potential loss of info is explicitly directed.To print the value of an integer:
There are additional issues about printing such as using
chuxchux%hhu
or casting when printing an unsigned char
, but leave that for later. There is a lot to printf()
.86.7k88 gold badges7777 silver badges161161 bronze badges
nhgrifnhgrif50.1k2121 gold badges105105 silver badges151151 bronze badges
Casting the integer to a char will do what you want.
There is no difference between 'a' and 97 besides the way you interperet them.
DanChianucciDanChianucci97511 gold badge1010 silver badges2020 bronze badges
kannadasankannadasan
Program Converts ASCII to Alphabet
Program Converts Alphabet to ASCII code
Amol DamodarAmol Damodar
Not the answer you're looking for? Browse other questions tagged cintegercharacter or ask your own question.
< cpp | language
C++Language | ||||
Standard Library Headers | ||||
Freestanding and hosted implementations | ||||
Named requirements | ||||
Language support library | ||||
Concepts library(C++20) | ||||
Diagnostics library | ||||
Utilities library | ||||
Strings library | ||||
Containers library | ||||
Iterators library | ||||
Ranges library(C++20) | ||||
Algorithms library | ||||
Numerics library | ||||
Input/output library | ||||
Localizations library | ||||
Regular expressions library(C++11) | ||||
Atomic operations library(C++11) | ||||
Thread support library(C++11) | ||||
Filesystem library(C++17) | ||||
Technical Specifications |
General topics | ||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||
Flow control | ||||||||||||||||||||||||||||||||||||||||||||||
Conditional execution statements | ||||||||||||||||||||||||||||||||||||||||||||||
Iteration statements (loops) | ||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||
Jump statements | ||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||
Functions | ||||||||||||||||||||||||||||||||||||||||||||||
Function declaration | ||||||||||||||||||||||||||||||||||||||||||||||
Lambda function declaration | ||||||||||||||||||||||||||||||||||||||||||||||
inline specifier | ||||||||||||||||||||||||||||||||||||||||||||||
Exception specifications(until C++20) | ||||||||||||||||||||||||||||||||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||||||||||||||||||||||||||||||||
Exceptions | ||||||||||||||||||||||||||||||||||||||||||||||
Namespaces | ||||||||||||||||||||||||||||||||||||||||||||||
Types | ||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||
Specifiers | ||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||
Storage duration specifiers | ||||||||||||||||||||||||||||||||||||||||||||||
Initialization | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
Expressions | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
Alternative representations | |||||||||||||||||||||||||||||||
Literals | |||||||||||||||||||||||||||||||
Boolean - Integer - Floating-point | |||||||||||||||||||||||||||||||
Character - String - nullptr (C++11) | |||||||||||||||||||||||||||||||
User-defined(C++11) | |||||||||||||||||||||||||||||||
Utilities | |||||||||||||||||||||||||||||||
Attributes(C++11) | |||||||||||||||||||||||||||||||
Types | |||||||||||||||||||||||||||||||
typedef declaration | |||||||||||||||||||||||||||||||
Type alias declaration(C++11) | |||||||||||||||||||||||||||||||
Casts | |||||||||||||||||||||||||||||||
Implicit conversions - Explicit conversions | |||||||||||||||||||||||||||||||
static_cast - dynamic_cast | |||||||||||||||||||||||||||||||
const_cast - reinterpret_cast | |||||||||||||||||||||||||||||||
Memory allocation | |||||||||||||||||||||||||||||||
Classes | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
Class-specific function properties | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
Special member functions | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Templates | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
Miscellaneous |
The following chart contains all 128 ASCII decimal (dec), octal (oct), hexadecimal (hex) and character (ch) codes.
dec | oct | hex | ch | dec | oct | hex | ch | dec | oct | hex | ch | dec | oct | hex | ch |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 00 | NUL (null) | 32 | 40 | 20 | (space) | 64 | 100 | 40 | @ | 96 | 140 | 60 | ` |
1 | 1 | 01 | SOH (start of header) | 33 | 41 | 21 | ! | 65 | 101 | 41 | A | 97 | 141 | 61 | a |
2 | 2 | 02 | STX (start of text) | 34 | 42 | 22 | ' | 66 | 102 | 42 | B | 98 | 142 | 62 | b |
3 | 3 | 03 | ETX (end of text) | 35 | 43 | 23 | # | 67 | 103 | 43 | C | 99 | 143 | 63 | c |
4 | 4 | 04 | EOT (end of transmission) | 36 | 44 | 24 | $ | 68 | 104 | 44 | D | 100 | 144 | 64 | d |
5 | 5 | 05 | ENQ (enquiry) | 37 | 45 | 25 | % | 69 | 105 | 45 | E | 101 | 145 | 65 | e |
6 | 6 | 06 | ACK (acknowledge) | 38 | 46 | 26 | & | 70 | 106 | 46 | F | 102 | 146 | 66 | f |
7 | 7 | 07 | BEL (bell) | 39 | 47 | 27 | ' | 71 | 107 | 47 | G | 103 | 147 | 67 | g |
8 | 10 | 08 | BS (backspace) | 40 | 50 | 28 | ( | 72 | 110 | 48 | H | 104 | 150 | 68 | h |
9 | 11 | 09 | HT (horizontal tab) | 41 | 51 | 29 | ) | 73 | 111 | 49 | I | 105 | 151 | 69 | i |
10 | 12 | 0a | LF (line feed - new line) | 42 | 52 | 2a | * | 74 | 112 | 4a | J | 106 | 152 | 6a | j |
11 | 13 | 0b | VT (vertical tab) | 43 | 53 | 2b | + | 75 | 113 | 4b | K | 107 | 153 | 6b | k |
12 | 14 | 0c | FF (form feed - new page) | 44 | 54 | 2c | , | 76 | 114 | 4c | L | 108 | 154 | 6c | l |
13 | 15 | 0d | CR (carriage return) | 45 | 55 | 2d | - | 77 | 115 | 4d | M | 109 | 155 | 6d | m |
14 | 16 | 0e | SO (shift out) | 46 | 56 | 2e | . | 78 | 116 | 4e | N | 110 | 156 | 6e | n |
15 | 17 | 0f | SI (shift in) | 47 | 57 | 2f | / | 79 | 117 | 4f | O | 111 | 157 | 6f | o |
16 | 20 | 10 | DLE (data link escape) | 48 | 60 | 30 | 0 | 80 | 120 | 50 | P | 112 | 160 | 70 | p |
17 | 21 | 11 | DC1 (device control 1) | 49 | 61 | 31 | 1 | 81 | 121 | 51 | Q | 113 | 161 | 71 | q |
18 | 22 | 12 | DC2 (device control 2) | 50 | 62 | 32 | 2 | 82 | 122 | 52 | R | 114 | 162 | 72 | r |
19 | 23 | 13 | DC3 (device control 3) | 51 | 63 | 33 | 3 | 83 | 123 | 53 | S | 115 | 163 | 73 | s |
20 | 24 | 14 | DC4 (device control 4) | 52 | 64 | 34 | 4 | 84 | 124 | 54 | T | 116 | 164 | 74 | t |
21 | 25 | 15 | NAK (negative acknowledge) | 53 | 65 | 35 | 5 | 85 | 125 | 55 | U | 117 | 165 | 75 | u |
22 | 26 | 16 | SYN (synchronous idle) | 54 | 66 | 36 | 6 | 86 | 126 | 56 | V | 118 | 166 | 76 | v |
23 | 27 | 17 | ETB (end of transmission block) | 55 | 67 | 37 | 7 | 87 | 127 | 57 | W | 119 | 167 | 77 | w |
24 | 30 | 18 | CAN (cancel) | 56 | 70 | 38 | 8 | 88 | 130 | 58 | X | 120 | 170 | 78 | x |
25 | 31 | 19 | EM (end of medium) | 57 | 71 | 39 | 9 | 89 | 131 | 59 | Y | 121 | 171 | 79 | y |
26 | 32 | 1a | SUB (substitute) | 58 | 72 | 3a | : | 90 | 132 | 5a | Z | 122 | 172 | 7a | z |
27 | 33 | 1b | ESC (escape) | 59 | 73 | 3b | ; | 91 | 133 | 5b | [ | 123 | 173 | 7b | { |
28 | 34 | 1c | FS (file separator) | 60 | 74 | 3c | < | 92 | 134 | 5c | | 124 | 174 | 7c | | |
29 | 35 | 1d | GS (group separator) | 61 | 75 | 3d | = | 93 | 135 | 5d | ] | 125 | 175 | 7d | } |
30 | 36 | 1e | RS (record separator) | 62 | 76 | 3e | > | 94 | 136 | 5e | ^ | 126 | 176 | 7e | ~ |
31 | 37 | 1f | US (unit separator) | 63 | 77 | 3f | ? | 95 | 137 | 5f | _ | 127 | 177 | 7f | DEL (delete) |
Note: in Unicode, the ASCII character block is known as
U+0000..U+007F
Basic Latin.[edit]See also
Retrieved from 'https://en.cppreference.com/mwiki/index.php?title=cpp/language/ascii&oldid=95383'