Link to home
Start Free TrialLog in
Avatar of klax33
klax33

asked on

Converting between char and int types

I have read input from a file and VC has it's ascii int value as well as it's char value.  That is to say that:

Name    Value
input   50 '2'

shows up in the Watch Box when I debug.  The problem is that I need to print '2' not '50'.  Input was declared of type char and I thought doing:

int newVal = (char)input;

would work, but it still prints 50.  I was thinking a type-cast did it, but I tried all that I knew.  Any help would be appreciated.
Avatar of arindam_bh
arindam_bh

there is no difference between char value and ascii value because, the system identifies the character by its integer ascii value.
The problem is the type of newVal:

char newVal = (char)input;

or, equivalently

char newVal = input;

The output routines know how to handle short/int/long (convert to a digit string in the current base) and char (convert to appropriate character according to colating sequence).

If you want to read input as a numeric value and only print it as a char, you can cast it on the fly in the output statement:

cout << (char)input << endl;

Hope this helps,
-bcl
Avatar of klax33

ASKER

bcladd,

Actually, the value I am reading, (50 '2') in this case, is being passed as a parameter to a vector<int> object.  So would I need to do anything differently because of that?  Your idea still prints out a 50x50 array this case instead of my 2x2 test array.  Thanks for your quick response.
Avatar of klax33

ASKER

Now I know I must do something different, because when I cout the value it says 2, but it still creates an array of size 50.
Note: (50 '2') is what you are seeing in the debugger. The debugger is "smart" and knows that char are somethimes used as little integers and sometimes used as characters. Thus it is showing you both values: 50 or the bit pattern 00011001 treated as a decimal number and '2', the same bit pattern interpreted as an ASCII character.

Remember that a char can always be stored in a short/int/long: they have more bits than the char and the char can just be sign-extended into the larger representation.

So you can use a char whenever an int (or rather an r-value of an int) is expected. So you can store char in a vector of int:

char ch = 50;
vector<int> v;
char ch2;

v.push_back(ch); // legal

ch2 = v[0]; // legal but silently truncates upper bits from int

Hope this comes close to answering your question.

-bcl
Now I understand what you are asking. If you read the value from the file into a char, the char will be the ASCII value of the character in the file.

So either read the value as an integer (if you expect the file to contain a sequence of digits representing a number) or read it into a char and then convert the char to its integer value.

A char containing a digit can be converted to its value using the following expression:

char ch = '6';
int digitValue = ch - '0'; // subtract the value of the digit zero

digitValue contains the integer value of 6. This works because the digits are sequential in the ASCII colating sequence.

-bcl
probably what you need is to convert the string "2" to the integer number 2?

try this:

int x;

cin >> x;

x should now be 2 if you typed 2.

What is going on behind the scenes here is that cin >> operator reads input as a string and then converts that string to an int. There are many ways to do that. If you have only one digit like '2' the easy way is to:

int x = ch - '0';

if ch is '0'..'9' then x will be 0..9

Normally you don't have only one digit but several digits that make up a number and then you can go in a loop and read digits and compute:

n = n * 10 + (ch - '0');

Fortunately there's functions in C and C++ that does that already:

char * e;
int x = strtol("237xyz", & e, 10);

after this call, x will have the value 237 and e will point to the string "xyz". Often you don't need that pointer e to analyze text after the number and in that case you can do:

int x = strtol("237", NULL, 10);

There's a function called atoi() that does exactly that so:

int x = atoi("237");

also works and x is 237 after that call.

However, this require that you read the whole string and not just one digit.

The easy way in C++ is to just use:

int x;

cin >> x;

to read the integer value.

Alf
Avatar of klax33

ASKER

bcladd,

That comes closer to answering my question, but the problem isn't adding that value to an array, it is making an array *of* that size.  For example:

char max_index = (char)largest_index;

vector< vector<int> > matrix(max_index);

for (int h = 0; h < max_index; h++)
     matrix[h].reserve(max_index);  

// set all indices to 0
for (int i = 0; i < max_index; i++)
{
     for (int j = 0; j < max_index; j++)
     matrix[i][j] = 0;
}

That snippet creates a 50x50 array instead of a 2x2, even though when I cout max_index it prints '2'.  

Note:  I have increased the points on this question to 150 for the added complexity.
ASKER CERTIFIED SOLUTION
Avatar of bcladd
bcladd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want an array of that size then you have to either set that size in the constructor of the array or you have to resize the array.

First off, if you want just 2 instead of 50 you should subtract the '0' as said earlier. If you are sure the matrix never have more than one digit size than that's what you can do. If it is more than one digit you should do something like this:

int x;

cin >> x;

x is now the input value (2 or whatever).

create a matrix:

vector< vector<int> > matrix(x);
for (int i = 0; i < x; ++i)
   matrix[x].reserve(x);

The matrix should now be 2x2 if x == 2.

Alf
Avatar of klax33

ASKER

Thank you both very much for your help.  Problem resolved.