Solved

convert CString data to integer

Posted on 2004-04-26
14
37,606 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 96
if loop in java 3 142
while loop over for loop 7 105
Capture logon name 13 73
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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