-
February 10th, 2010, 02:23 AM
#1
Problem when Converting from CString to int
I am new on Using with dotnet platform. Previously I have Experience on working with VC++ 6.0.
On this exp, I hav choose VC++.Net for my current Project... By understanding the UNICODE fromats, I was going on right way(Extremely with the helps of CodeGuru team)
Here I am receiving some Datas from an Extenal PC Board via Serial Port.
The Data Receiving as ASCII format and I have stored it on a CString FirstData.
The following codes explains it.
Code:
CString FirstData;// already declared and here reads Data from Serial Port
for(int i=0;i<FirstData.GetLength();i++)
{
int ibyte;
CString ll;
ll=FirstData.GetAt(i);
sscanf(ll,"%x",&ibyte)
}
Here I wanna convert this CString value ll to an int Hex Format and print it on a file....
But it is not working here.error Message came as "Cannot convert parameter 1 from CString to const char*.
Next I have changed sscanf as
Code:
_stscanf(ll,_T("%x),&ibyte);
It has worked...But when I read the data ibyte..it is wrong(some error datas are printed)
Again I used atoi function--still error values came.
How do I convert the exact data to an int Format.plz help...
Last edited by arunkr6; February 10th, 2010 at 04:10 AM.
-
February 10th, 2010, 04:20 AM
#2
Re: Problem when Converting from CString to int
Sure, "char cannot be convert to a char* (as well as any other object cannot be converted to its pointer!)
You must understand:
1. CString::GetAt() returns NOT a string but a single character (char)
2. Use sscanf in MFC - is nonsense! There are more simple and appropriate functions, such as _ttoi, _tcstoul, _tcstol
3. All these convertion functions require a pointer to the buffer containing text to convert, so if you need/want to convert only one character you can use CString::Mid to extract a substring with only this one character:
Code:
CString FirstData;// already declared and here reads Data from Serial Port
for(int i=0;i<FirstData.GetLength();i++)
{
int ibyte = _ttoi( FirstData.Mid(i, 1));
}
Victor Nijegorodov
-
February 10th, 2010, 04:34 AM
#3
Re: Problem when Converting from CString to int
thank u VictorN,
further, if I want to write this ibyte on a file...
Can I use fprintf function????
-
February 10th, 2010, 04:38 AM
#4
Re: Problem when Converting from CString to int
Well, if you want to ignore MFC advantages (CFile, CStdioFile classes) then yes, of course you can use fprintf function.
Victor Nijegorodov
-
February 10th, 2010, 06:02 AM
#5
Re: Problem when Converting from CString to int
Thank u...
The error solved bt on printing in file, I've coded as
Code:
_ftprintf(file1,_T("%.2X"),ibyte);
each data printed as 0 only.. not the exact converted forms of getting values in CString.
So I've confirmed the ibyte value by formatted on CString.Since all the values are 0 only..
wy so?? I'm confused!!!!!
-
February 10th, 2010, 07:12 AM
#6
Re: Problem when Converting from CString to int
Perhaps, you are printing the only 2 high bytes os ibyte?
Well, the main question is: what are you trying to do and why do do it so complicated?
Code:
CString FirstData;// already declared and here reads Data from Serial Port
for(int i=0;i<FirstData.GetLength();i++)
{
_ftprintf(file1,_T("%.2X"), FirstData[i]);
}
Victor Nijegorodov
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|