Convertinng Ascii to CString not working in VC6.0, working fine VS2008
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Convertinng Ascii to CString not working in VC6.0, working fine VS2008

  1. #1
    Join Date
    Aug 2012
    Posts
    21

    Convertinng Ascii to CString not working in VC6.0, working fine VS2008

    Hi!

    The below code is working fine in VS2008 and not working in VC6.0 (taking garbage values)
    this code is for converting hex values to string.

    sending Input string is : 727332333263 required output: rs232c

    DWORD AsciiToString(LPCTSTR f_chInputChar, LPTSTR f_chOutputChar)
    {
    long ch;
    int i,j;
    TCHAR tmp[2];

    int len = _tcslen(f_chInputChar);
    for(i = j= 0; i < len; i += 2, j++){
    memset(tmp, NULL, 2);
    tmp[0] = f_chInputChar[i];
    tmp[1] = f_chInputChar[i+1];

    ch = _tcstol(tmp, NULL, 16);
    f_chOutputChar[j] = (TCHAR)ch;
    }
    return TRUE;
    }

    Please suggest the required changes.


    Thanks in advance to all!

    regards
    sam.
    Last edited by mr.sam1024; September 22nd, 2012 at 08:42 AM.

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,307

    Re: Convertinng Ascii to CString not working in VC6.0, working fine VS2008

    Where is the CString object? I don't see one being used.

  3. #3
    Join Date
    Aug 2012
    Posts
    21

    Re: Convertinng Ascii to CString not working in VC6.0, working fine VS2008

    there is no cstring object.. input is LPCTSTR and output is LPCTSTR.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,567

    Re: Convertinng Ascii to CString not working in VC6.0, working fine VS2008

    Code:
    TCHAR tmp[2];
     
    int len = _tcslen(f_chInputChar);
    for (i = j= 0; i < len; i += 2, j++)
    {
       memset(tmp, NULL, 2);
       tmp[0] = f_chInputChar[i];
       tmp[1] = f_chInputChar[i+1];
     
      ch = _tcstol(tmp, NULL, 16);
    Just a quick look at your code ...

    _tcstol() expects a NULL terminated string as input.

    At the very least, you need to dimension tmp at 3 and set
    tmp[2] = NULL.

    Also, you need to either explicity NULL terminate f_chOutputChar
    in the function or the calling routine.

  5. #5
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,307

    Re: Convertinng Ascii to CString not working in VC6.0, working fine VS2008

    Quote Originally Posted by mr.sam1024 View Post
    there is no cstring object.. input is LPCTSTR and output is LPCTSTR.
    I guess I'm confused then since the title of your post is "Convertinng Ascii to CString not working in VC6.0, working fine VS2008".

    At any rate, why not use the string conversion macros?

  6. #6
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,026

    Re: Convertinng Ascii to CString not working in VC6.0, working fine VS2008

    Although it has not the wisest implementation (see the previous remarks), your function works in VS6.0 as well.
    Probably, you are seeing "garbage" string in watch window in a UNICODE build.
    To fix that, have a look at this old FAQ: How to display UNICODE strings in Watch/QuickWatch window.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center