• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 37646
  • Last Modified:

convert CString data to integer

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
abi_a
Asked:
abi_a
  • 4
  • 4
  • 3
  • +1
1 Solution
 
ZoppoCommented:
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
 
akalmaniCommented:
Alternately you can also use
int iVal = 0;
sscanf((LPCTSTR)data, &iVal);
0
 
abi_aAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ZoppoCommented:
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
 
akalmaniCommented:
seems strange for me too..
maybe you can try this
sscanf(data.GetBuffer(data.GetLength()), &iVal);
0
 
vijay_visanaCommented:
can you show us some code
0
 
abi_aAuthor Commented:
Am getting same error.Iam coding in embedded vc++. Does that make any difference.

thanks
0
 
ZoppoCommented:
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
 
abi_aAuthor Commented:
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
 
vijay_visanaCommented:


num = _ttoi((LPCTSTR)data);


or

TCHAR sz[1024];

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



0
 
vijay_visanaCommented:
OPPSs...

TCHAR sz[1024];

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

0
 
akalmaniCommented:
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
 
akalmaniCommented:
Sorry too late in submission...did not refresh before submitting
0
 
abi_aAuthor Commented:

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

thanks very much
0

Featured Post

Technology Partners: 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!

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now