Problem when Converting from CString to int
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Problem when Converting from CString to int

  1. #1
    Join Date
    Oct 2008
    Posts
    59

    Question 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,"&#37;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 03:10 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,291

    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

  3. #3
    Join Date
    Oct 2008
    Posts
    59

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

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,291

    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

  5. #5
    Join Date
    Oct 2008
    Posts
    59

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

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,291

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center