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
Solved

TCHAR to int

Posted on 2007-11-19
10
4,040 Views
Last Modified: 2013-12-14
Ah hello.

This seems like it should have a simple solution, but I cannot find it.  I have a single TCHAR, obtained from CString::GetAt.  I need to convert it to an integer.

I cannot use _ttoi as that requires a TCHAR* not a TCHAR.

How can I convert a single character?

TIA
0
Comment
Question by:mrwad99
  • 4
  • 4
  • 2
10 Comments
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20312518
ttoi() -  did you try this?

Best regards,
DeepuAbrahamK
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20312523
just do a casting operation:

TCHAR a = somestring.GetAt(5);
int i = (int)a;
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20312538


int i = _ttoi((LPCTSTR)tchardata);

Try this way of casting as well
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 19

Author Comment

by:mrwad99
ID: 20312563
Jaime:

      CString str ( _T("1") );
      TCHAR ch = str.GetAt ( 0 );
      int n34 = ( TCHAR ) ch;

This sets 'n34' to 49, not 1.

DeepuAbrahamK:

      CString str ( _T("1") );
      TCHAR ch = str.GetAt ( 0 );
      int n34 = _ttoi ( ( LPCTSTR )ch );
      

This crashes.

???
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 150 total points
ID: 20312573
not sure what you need
if you want to convert just 1 character to int, then:
int i = (int)a - 48;
0
 
LVL 19

Author Comment

by:mrwad99
ID: 20312590
>> if you want to convert just 1 character to int, then:

Yes that is exactly what I need.  I get a TCHAR from CString::GetAt(), which I then need to convert to an int.  As I said above, I cannot use _ttoi as that requires a TCHAR* parameter, not a TCHAR.

>> int i = (int)a - 48;

That does the trick.  Can you explain why please, and can I 100% rely on this with UNICODE/non UNICODE apps ?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20312654
the '0'  digit has always a value of 48, in both ASCII/ANSI and Unicode.
following numbers values are 49,50,etc.
So, this always will be true.

Just you have to handle what to do, when the caracter is not a digit, that is, with a value lower and 48 or greater than 57

0
 
LVL 19

Author Comment

by:mrwad99
ID: 20312983
OK, after consideration I understand that: if I output a character A via

int i = (int)a - 48;

I get 17.  Hence the need for the range test.  Interestingly, after looking on http://www.asciitable.com/, I see that 17 is not the decimal of 'A'.  Can you tell me why this please?  (Points up 50 now to 150 for this extra question)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20313086
>>I see that 17 is not the decimal of 'A'.
I'm not sure on what you expect to have in this case.  Which is the decimal of A?
0
 
LVL 19

Author Comment

by:mrwad99
ID: 20313113
Sorry, ignore me.  I was forgetting we had subtracted 48.  (The int equivalent of A is 65, but we subtracted 48 from that to give 17.)

Looking at the asciitable I see that characters 0 - 9 are in the range 48 - 57 (inclusive), which is why we do the range test.

All is clear now :)

Thanks a lot :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 831
Problem with SqlConnection 4 177
Recommendation vb6 to vb.net or others 14 174
learn programming 8 32
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 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