Solved

convert CString data to integer

Posted on 2004-04-26
14
37,609 Views
Last Modified: 2013-11-20
HI,
     How to convert a CString data to int. Suppose I have

char* bytearray;
CString data;
CString CSbytearray(bytearray);
data = CSbytearray.Mid(1,4);

Now,I need data in integer format. Can anyone help.
0
Comment
Question by:abi_a
  • 4
  • 4
  • 3
  • +1
14 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 10917693
Hi abi_a,


if I understand correctly you have a string containing a number which you want to have as an 'int', correct?

for this you can use atoi function for this, i.e.

int num = atoi( (LPCTSTR)data );

hope that helps,

ZOPPO
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10917826
Alternately you can also use
int iVal = 0;
sscanf((LPCTSTR)data, &iVal);
0
 

Author Comment

by:abi_a
ID: 10917983
hi,
 
For  int num = atoi( (LPCTSTR)data ); I get compiler error
cannot convert from 'class CString' to 'const char *'

for sscanf((LPCTSTR)data, &iVal); getting
'sscanf' : cannot convert parameter 1 from 'const unsigned short *' to 'const char *'

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 31

Expert Comment

by:Zoppo
ID: 10918122
hm ... that's strange ... try these:

num = atoi( data );
num = atoi( (LPTSTR)data );

BTW, I find this is strange: LPCTSTR of a CString should always work since LPCTSTR
is a conversion operator of CString class in MFC
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10918160
seems strange for me too..
maybe you can try this
sscanf(data.GetBuffer(data.GetLength()), &iVal);
0
 
LVL 7

Expert Comment

by:vijay_visana
ID: 10918263
can you show us some code
0
 

Author Comment

by:abi_a
ID: 10918281
Am getting same error.Iam coding in embedded vc++. Does that make any difference.

thanks
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 10918368
Well, I guess so ... I only found in MSDN that MFC for Windows CE CString has an operator LPCTSTR ...

maybe you can call it in another way, i.e. as function instead as operator
num = atoi( LPCTSTR( data ) );

Unfortunateley I have no further experience with embedded vc++, sorry ...

ZOPPO
0
 

Author Comment

by:abi_a
ID: 10918400
my code goes like this.....

CString data;
CString CSbytearray(bytearray);  // bytearray is a char array
data = CSbytearray.Mid(1,4);
 
In data I get something like 12 as a string. I need to convert it to int.
0
 
LVL 7

Accepted Solution

by:
vijay_visana earned 500 total points
ID: 10918511


num = _ttoi((LPCTSTR)data);


or

TCHAR sz[1024];

lstrcpy(sz, data);
num =  _ttoi((LPCTSTR)data);



0
 
LVL 7

Expert Comment

by:vijay_visana
ID: 10918521
OPPSs...

TCHAR sz[1024];

lstrcpy(sz, data);
num =  _ttoi((LPCTSTR)sz);

0
 
LVL 3

Expert Comment

by:akalmani
ID: 10918544
Seems that CString is using UNICODE and the other functions we mentioned are just using ANSI
Last try from me since I too do not have any experience in eVC++

int iVal = 0;
_stscanf(data.GetBuffer(data.GetLength()), _T("%d"), &iVal);

Slight modification to Zoppo's solution, try something like this
iVal = _ttoi(data.GetBuffer(data.GetLength()));
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10918552
Sorry too late in submission...did not refresh before submitting
0
 

Author Comment

by:abi_a
ID: 10918706

well     num = _ttoi((LPCTSTR)data);
  works fine

thanks very much
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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