30

How can I convert an ASCII character into an int in C?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
node ninja
  • 31,796
  • 59
  • 166
  • 254

8 Answers8

27

What about:

int a_as_int = (int)'a';
rjnilsson
  • 2,343
  • 15
  • 20
  • 2
    I'll add the necessary reference to the argument: http://stackoverflow.com/questions/433895/why-are-c-character-literals-ints-instead-of-chars – xanatos Mar 16 '11 at 12:34
19

I agree to Ashot and Cwan, but maybe you like to convert an ascii-cipher like '7' into an int like 7?

Then I recoomend:

char seven = '7';
int i = seven - '0'; 

or, maybe you get a warning,

int i = (int) (seven - '0'); 

corrected after comments, thanks.

user unknown
  • 35,537
  • 11
  • 75
  • 121
16

Are you searching for this:

int c = some_ascii_character;

Or just converting without assignment:

(int)some_ascii_character;
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
UmmaGumma
  • 5,633
  • 1
  • 31
  • 45
11

Use the ASCII to Integer atoi() function which accepts a string and converts it into an integer:

#include <stdlib.h>

int num = atoi("23"); // 23

If the string contains a decimal, the number will be truncated:

int num = atoi("23.21"); // 23
Anas Abu Farraj
  • 1,540
  • 4
  • 23
  • 31
6

A char value in C is implicitly convertible to an int. e.g, char c; ... printf("%d", c) prints the decimal ASCII value of c, and int i = c; puts the ASCII integer value of c in i. You can also explicitly convert it with (int)c. If you mean something else, such as how to convert an ASCII digit to an int, that would be c - '0', which implicitly converts c to an int and then subtracts the ASCII value of '0', namely 48 (in C, character constants such as '0' are of type int, not char, for historical reasons).

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
2

You mean the ASCII ordinal value? Try type casting like this one:

int x = 'a';
Jhourlad Estrella
  • 3,545
  • 4
  • 37
  • 66
  • @Jim Balter Sorry if it's obvious but I was wondering how this is c++ as opposed to c. Is it because of the terminology or am I missing something? – flight Mar 16 '11 at 07:59
  • @quasiverse Look at the edit -- that is, click the "43 mins ago" link. Note that my comment was posted 48 minutes ago. Beware of this in general -- it will bite you at SO. – Jim Balter Mar 16 '11 at 08:06
  • Oops, silly me, those are the times I saw when I posted that but of course those aren't the times that people will see when reading it. – Jim Balter Mar 16 '11 at 11:59
0

It is not possible with the C99 standard library, unless you manually write a map from character constants to the corresponding ASCII int value.

Character constants in C like 'a' are not guaranteed to be ASCII.

C99 only makes some guarantees about those constants, e.g. that digits be contiguous.

The word ASCII only appears on the C99 N1256 standard draft in footer notes, and footer note 173) says:

In an implementation that uses the seven-bit US ASCII character set, the printing characters are those whose values lie from 0x20 (space) through 0x7E (tilde); the control characters are those whose values lie from 0 (NUL) through 0x1F (US), and the character 0x7F (DEL).

implying that ASCII is not the only possibility

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0

As everyone else told you, you can convert it directly... UNLESS you meant something like "how can I convert an ASCII Extended character to its UTF-16 or UTF-32 value". This is a TOTALLY different question (one at least as good). And one quite difficult, if I remember correctly, if you are using only "pure" C. Then you could start here: https://stackoverflow.com/questions/114611/what-is-the-best-unicode-library-for-c/114643#114643

(for ASCII Extended I mean one of the many "extensions" to the ASCII set. The 0-127 characters of the base ASCII set are directly convertible to Unicode, while the 128-255 are not.). For example ISO_8859-1 http://en.wikipedia.org/wiki/ISO_8859-1 is an 8 bit extensions to the 7 bit ASCII set, or the (quite famous) codepages 437 and 850.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • 2
    ASCII (0-127) characters in any UTF format are identical to their integer representation. – Chris Lutz Mar 16 '11 at 07:24
  • @Chris But at least 50% of times, when someone speaks of ASCII he means "ASCII Extended"... But yes, it would be interesting to put this note in the reply. Thanks. – xanatos Mar 16 '11 at 07:27
  • ASCII characters range in value from 0 to 127, so converting them to UTF-16 or UTF-32 is just a cast. Perhaps you mean something different, such as converting various byte encodings like UTF-8, ISO 8859-1 etc. – Jim Balter Mar 16 '11 at 07:28
  • "ASCII extended" is a poor term that does not have a unique referent, or even close; there are numerous "extended" ASCII encodings, or "code pages" as some proprietary-minded commercial vendors call them. – Jim Balter Mar 16 '11 at 07:31