-
September 22nd, 2012, 08:40 AM
#1
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.
-
September 22nd, 2012, 10:49 AM
#2
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.
-
September 22nd, 2012, 11:20 AM
#3
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.
-
September 22nd, 2012, 12:20 PM
#4
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.
-
September 23rd, 2012, 11:08 PM
#5
Re: Convertinng Ascii to CString not working in VC6.0, working fine VS2008
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?
-
September 24th, 2012, 08:20 AM
#6
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.
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
|