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

  • Thread starter Peon666
  • Start date
In summary, the conversation discusses a code that is supposed to convert a character array into an integer array. However, when the array is outputted, it shows unexpected numbers. The expert suggests that the issue may be due to the array not being initialized and suggests initializing it with sensible values. They also mention that characters are represented by ASCII codes and provide methods for converting characters into integers. The conversation continues to discuss how to handle more complicated parsing problems. Ultimately, the expert suggests using functions like sscanf() and strtol() for parsing.
  • #1
Peon666
108
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
  • #2
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.
 
  • #3
It's arr2[10]="P 1 (a tab) 0";
 
  • #4
And it outputs: 80 49 9 48 0 0 0 0 0 0

I want to get 1 and 0 as integers
 
  • #5
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/).
 
  • #6
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.
 
  • #7
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?
 
  • #8
You need to learn how to parse. Casting is not what you want here.
 
  • #9
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.
 

1. What is the difference between a char and an int?

A char is a single character, while an int is a numerical value. Char values are represented by their corresponding ASCII or Unicode codes, while int values can be any whole number within a specific range.

2. Why would I need to convert from char to int?

There are several reasons why you might need to convert from char to int. One common reason is to perform mathematical operations on char values, as they cannot be used in calculations. Another reason is to compare char values to int values, as their data types are different.

3. How do I convert a char to an int in C++?

In C++, you can use the built-in function int(charValue) to convert a char value to its corresponding int value. This function takes the ASCII or Unicode code of the char as its argument and returns the corresponding int value.

4. Can I convert a string to an int using the same method?

No, you cannot convert a string to an int using the int() function. Strings are made up of multiple characters, so they cannot be converted using a single ASCII or Unicode code. To convert a string to an int, you can use the stoi() function in C++, which takes a string as its argument and returns the corresponding int value.

5. What happens if I try to convert a non-numeric char to an int?

If you try to convert a non-numeric char, such as a letter or symbol, to an int, the resulting value will be based on the ASCII or Unicode code for that character. For example, the char 'A' has an ASCII code of 65, so converting it to an int would result in the value 65.

Similar threads

  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
753
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top