?
Solved

Converting between char and int types

Posted on 2003-03-10
11
Medium Priority
?
823 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:klax33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 

Expert Comment

by:arindam_bh
ID: 8103358
there is no difference between char value and ascii value because, the system identifies the character by its integer ascii value.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8103410
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
0
 

Author Comment

by:klax33
ID: 8103463
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:klax33
ID: 8103748
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.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8103752
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
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8103778
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
0
 
LVL 12

Expert Comment

by:Salte
ID: 8103800
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
0
 

Author Comment

by:klax33
ID: 8103827
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.
0
 
LVL 11

Accepted Solution

by:
bcladd earned 600 total points
ID: 8104061
I think my second answer above fixes your problem. That is, if you know you read largest_index as a char (see Salte's answer for a complete discussion of reading it as an integer) and it therefore contains the ASCII code of a single digit, the follwoing should fix up your code:

assert(isdigit(largest_index)); // not necessary but it makes sure we have '0'-'9'

max_index = largest_index - '0';

// rest unchnaged.

-bcl
0
 
LVL 12

Expert Comment

by:Salte
ID: 8104101
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
0
 

Author Comment

by:klax33
ID: 8107434
Thank you both very much for your help.  Problem resolved.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question