What are some solutions for properly converting char to int in C++?

  • Context: C/C++ 
  • Thread starter Thread starter Peon666
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the challenges of converting characters to integers in C++, particularly focusing on issues arising from uninitialized variables and the correct methods for parsing character arrays. Participants explore various approaches and functions for achieving the desired conversion.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant questions the output of an integer array derived from a character array, suggesting that uninitialized variables may be the cause.
  • Another participant points out that characters are represented by their ASCII values and suggests a method to convert character digits to integers by checking their range and subtracting '0'.
  • A participant clarifies the ASCII values of the characters in the provided string, indicating that the output is correct for ASCII representation but does not meet the participant's goal of obtaining integer values from the characters.
  • One participant mentions that simple casting is insufficient for the desired conversion and implies the need for a different approach.
  • Another participant raises a new issue regarding parsing a different string and notes that not all expected integers are being outputted, indicating a potential parsing problem.
  • Participants suggest using functions like sscanf() and strtok() for parsing strings and converting tokens into integers, emphasizing the need for proper parsing techniques.

Areas of Agreement / Disagreement

Participants generally agree that casting is not the appropriate method for converting characters to integers and that parsing techniques are necessary. However, there is no consensus on the best approach to handle specific parsing scenarios, as different methods are proposed without agreement on their effectiveness.

Contextual Notes

Participants express uncertainty regarding the handling of uninitialized variables and the specifics of string parsing, indicating that assumptions about input formats and character representations may affect the outcomes.

Peon666
Messages
107
Reaction score
0
What could be wrong with this code?:

Code:
int arr[10];
char arr2[10];
for (int i=0; i<10; i++)
{
        arr[i]=(int)arr2[i];
}

When I output the supposedly converted array, it's outputs some crazy numbers.
?
 
Technology news on Phys.org
I assume you are using C/C++ here, in which case you should note that automatic variables (variables defined on the stack) are not initialized. Try initialize your character array with some sensible values.
 
It's arr2[10]="P 1 (a tab) 0";
 
And it outputs: 80 49 9 48 0 0 0 0 0 0

I want to get 1 and 0 as integers
 
Characters are internally represented by a byte that contains the code for the character in question. See for instance http://en.wikipedia.org/wiki/ASCII.

To get the digit of the character you would normally first test if the character is in the range '0' to '9' and then subtract '0'. Something like this:

Code:
char ch = ...;
if (ch >= '0' && ch <= '9') {
  int digit = ch - '0';
  // do whatever you need with digit
}

Alternatively you can use some of the methods from the standard ctype.h library (see for instance http://www.cplusplus.com/reference/clibrary/cctype/).
 
Peon666 said:
It's arr2[10]="P 1 (a tab) 0";

Peon666 said:
And it outputs: 80 49 9 48 0 0 0 0 0 0

80 = 'P'
49 = '1'
9 = tab
48 = '0'
0 = end of string

It is working exactly right, assuming you are working on an ASCII machine.

I want to get 1 and 0 as integers
Then you want to do something other than a simple cast.
 
Alright. The problem is solved to some extend. But a little more problem:

For the given string [P 1 (tab) 0] it outputs fine: 1 0
But when I have a string [P 2 (tab) 34] it outputs: 2 3 (no four.)

What could be the problem?
 
You need to learn how to parse. Casting is not what you want here.
 
To add to the above:

In this case, the function sscanf() will do the trick.

For more complicated parsing problems, you will probably need to learn how to use things like strtok() to split the string into tokens and things like strtol() to convert the numeric tokens into integers.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
89
Views
7K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K