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.
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.
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.
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.
Re: Convertinng Ascii to CString not working in VC6.0, working fine VS2008
Quote:
Originally Posted by
mr.sam1024
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?
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.