How to Convert Char to Int

Convert a character digit to the corresponding integer in C

As per other replies, this is fine:

char c = '5';
int x = c - '0';

Also, for error checking, you may wish to check isdigit(c) is true first. Note that you cannot completely portably do the same for letters, for example:

char c = 'b';
int x = c - 'a'; // x is now not necessarily 1

The standard guarantees that the char values for the digits '0' to '9' are contiguous, but makes no guarantees for other characters like letters of the alphabet.

How can I convert a char to int in Java?

The ASCII table is arranged so that the value of the character '9' is nine greater than the value of '0'; the value of the character '8' is eight greater than the value of '0'; and so on.

So you can get the int value of a decimal digit char by subtracting '0'.

char x = '9';
int y = x - '0'; // gives the int value 9

Convert char to int in C#

Interesting answers but the docs say differently:

Use the GetNumericValue methods to
convert a Char object that represents
a number to a numeric value type. Use
Parse and TryParse to convert a
character in a string into a Char
object. Use ToString to convert a Char
object to a String object.

http://msdn.microsoft.com/en-us/library/system.char.aspx

Convert from char * to int

This should help

#include <stdlib.h>

inline int to_int(const char *s)
{
return atoi(s);
}

just in case for an example usage

int main( int ac, char *av[] )
{
printf(" to_int(%s) = %d " ,av[1] , to_int(av[1]) );
return 0;
}

How To Turn Char of '2' into Integer 2 in C++

Just subtract the ASCII value of '0' from '2' to get the integer 2.

char c = '2';
int n = c - '0';

This is guaranteed to work even if the encoding is not ASCII, since the language requires that the encoding of the characters increases from '0' to '9'.

How to convert char to int, and vice versa?

Are you talking about a single digit character, like '1' or '9'? Then you would do something like this:

char c = '5';      
int x = c - '0'; // x == 5

Digit encodings are consecutive in all character character coding schemes (ASCII, EBCDIC, etc.), so subtracting the code for '0' gives you the right value.

Are you talking about converting the character encoding to an integer? Then all you need to do is assign the character value to an int:

int x = '5'; // x contains the character encoding of '5' - in ASCII, 53

Are you talking about converting a character string, like "12" or "42"? Then you would need to use one of the sscanf, strtol, or atoi library functions (or roll your own equivalent). Given

char str[] = "123";
int x;

you would do

sscanf( str, "%d", &x ); 

or

x = atoi( str ); 

or

char *chk;
x = (int) strtol( str, &chk, 10 );
if ( !isspace( *chk ) && *chk != 0 )
// str contained a non-numeric character, handle as appropriate

How to convert a single char into an int

You can utilize the fact that the character encodings for digits are all in order from 48 (for '0') to 57 (for '9'). This holds true for ASCII, UTF-x and practically all other encodings (see comments below for more on this).

Therefore the integer value for any digit is the digit minus '0' (or 48).

char c = '1';
int i = c - '0'; // i is now equal to 1, not '1'

is synonymous to

char c = '1';
int i = c - 48; // i is now equal to 1, not '1'

However I find the first c - '0' far more readable.

How do I convert a Char to Int?

That's because num is a Char, i.e. the resulting values are the ascii value of that char.

This will do the trick:

val txt = "82389235"
val numbers = txt.map { it.toString().toInt() }

The map could be further simplified:

map(Character::getNumericValue)

How can I convert char to int in c++ to get correct values of numbers?

To get real value subtract the initial value
like

char x='1'; //ascii value=49
int xx=x-'0' //49-48==1

char a='C'; //67
char aa=a-'A' //67-65=2 means third char of Alphabet

or you can type cast to get exact value

Hope you get the idea.



Related Topics



Leave a reply



Submit