[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

convert CString data to integer

Posted on 2004-04-26
14
Medium Priority
?
37,634 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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 …
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

656 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