Solved

convert CString data to integer

Posted on 2004-04-26
14
37,614 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
[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
  • 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
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!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
Login Script to Copy Folders 12 50
repeatSeparator  java  challenge 13 59
Problem to event 3 121
JQuery serialize and unserialize 8 264
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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