Melynda Anne Sherrill
Melynda Anne Sherrill

Reputation: 23

How to properly convert char to int in c++?

I'm getting a unique issue that I haven't experienced before when converting char to int in c++.

I am iterating through a string of integers called total_line which reads as:

1526

I'm trying to convert every other char into an int and here is my code:

cout << total_line[i+1] <<endl;
int d = total_line[i+1] <<endl;
cout << d << endl;
i++;

My output is strangely this:

5
53
6
54

I'm not sure why 5 is being converted to int 53 and 6 is being converted to int 54.

Upvotes: 1

Views: 4756

Answers (4)

Felix Glas
Felix Glas

Reputation: 15524

As others have mentioned, characters are actually represented as numbers which are mapped to a character table. The number-to-character map differs depending on the chosen charset, e.g., for US-ASCII the characters '1' and '2' correspond to the numbers 49 and 50 (see here for the full US-ASCII table).

To convert the string representation of a number into a signed integer use std::stoi (since C++11).

The following snippet will chop up the string into its individual digits and use std::stoi to convert them into numbers, respectively.

for (std::string::size_type i = 0, n = total_line.size(); i != n; ++i) {
    int d = std::stoi(total_line.substr(i, 1));

    std::cout << d << std::endl;
}

Using the standard-library function std::stoi has the advantage of working regardless of character encoding.

Upvotes: 3

Mathieu Van Nevel
Mathieu Van Nevel

Reputation: 1486

Just look at the ascii table (man ascii on linux)

     Oct   Dec   Hex   Char
     ---------------------
     060   48    30    0
     061   49    31    1
     062   50    32    2
     063   51    33    3
     064   52    34    4
  -> 065  |53|   35   |5|
  -> 066  |54|   36   |6|
     067   55    37    7 
     070   56    38    8 
     071   57    39    9  

So you can see that the reason why 5 is converted to 53 and 6 to 54, is because the decimal value of the char '5' is not 5 but 53 (same with 6).

If you want to convert char to int try something like this:

int d = total_line[i] - '0';

I won't recommend you this one :

int d = total_line[i] - 48;

Because of portability(thanks to Pete Becker), and not everybody know the decimal value of '0' and used directly '0' is more understandable I think.

Upvotes: 2

Pooya
Pooya

Reputation: 6136

You need to read from position i instead of i+1 and also there was an extra endl in the second line. The values you get is the ASCII value of the characters you try to read. In order to convert them to int you need to subtract the ASCII value of '0' from it. And also if you are operating in a for loop you don't need to increment i explicitly. That is maybe the reason you skip characters

This may help:

cout << total_line[i] <<endl;
int d = total_line[i];
cout << d - '0' << endl;

Upvotes: 1

abelenky
abelenky

Reputation: 64730

This line looks very strange:

int d = total_line[i+1] <<endl;

What is the stream operator<< and endl doing there?

Why not simply do this?

int d = total_line[i+1];

Upvotes: 1

Related Questions