Home > Enterprise >  Converting multi digit char to int in C
Converting multi digit char to int in C

Time:02-14

While I know that you can convert single digits that are characters to integers by doing, for example:

int main() 
{
    char var = '5';
    int num_var = var - 48;
}

I know you can't do the same thing for a character with a value which is say, '148'. Is there an easy way to convert this character value to an integer value of 148?

CodePudding user response:

You can use atoi(buffer) function, which returns the integer from the string buffer.

You need to include <stdlib.h>

Here is the prototype of the function :

int atoi (const char * str);

CodePudding user response:

You can't store more than one character in a char. Use a char array instead, like char var[] = "3232". Then use the atoi function to convert to integer. int val = atoi(var). Make sure to include the header <stdlib.h>.

CodePudding user response:

I know you can't do the same thing for a character with a value which is say, '148'. Is there an easy way to convert this character value to an integer value of 148?

Actually, the type of a (narrow) multicharacter literal is already int, so no conversion is needed to change the type (by converting the value to char, you may lose the precise value though since char cannot typically represent all values that int can). But the value of '148' is not necessarily (nor typically) 148, and there's no standard way to convert the value because the value of '148' is implementation defined.

It's of course possible to write a mapping using math for a particular language implementation if you know the mapping based on the documentation of that implementation, but such mapping isn't portable to other language implementations. Furthermore, multi-character literals are only conditionally supported, so a language implementation might not have them at all.

In conclusion: If you think that you need such conversion, then multicharacter literal is not the correct tool for the job.

CodePudding user response:

Case 1

First you should not use magic number like 48 etc because they make the program less portable. You should instead subtract '0' as shown below:

char var = '5';
int num_var = var - '0'; ////the result of var - '0' is guaranteed to be the integer 5

Here both var and '0' will be promoted to int. And the final result that is used to initialize variable num_var on the left hand side will be the result of subtraction of those two promoted int values on the right hand side. Moreover, the result is guaranteed to be the integer 5 since from C Standard (2.3 Character sets):

  1. ...In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

Case 2

Suppose we have more than one characters, then one way would be to use std::string and std::istringstream as shown below:

std::string var = "148";
int num_var;
std::istringstream ss(var);
ss >> num_var; //now num_var is the integer 148
  •  Tags:  
  • c
  • Related