English 中文(简体)
How to use TRACE with ascii under unicode MFC environment?
原标题:

I am developing a MFC program under windows CE. It is unicode by default. I can use TRACE to print some message like this

TRACE(TEXT("Hey! we got a problem!
"));

It works fine if everything is unicode. But however, I got some ascii string to print. For example:

// open the serial port
m_Context = CreateFile(TEXT("COM1:"), ...);
int rc = ReadFile(m_Context, buffer, 1, cBytes, NULL);
// Oops!! We got a problem, because we can t print a non-unicode string
TRACE(TEXT("Read data: %s
"), buffer);

I read string through com1 from a GPS module. It send text like this "$GPSGGA,1,2,3,4". They are all encoded with ASCII. I want to print them out with TRACE, how can I do?

Thanks.

最佳回答

Use "%hs" to format a narrow string argument, provided that you don t care about the code page. See, for example, this page for description of format specifiers.

问题回答

In Windows the "%S" format specifier (capital S ) will format a string that s the opposite of the build. In UNICODE builds it ll expect an ANSI/MBCS string and in non-UNICODE builds it ll expect a UNICODE argument.

I m not 100% sure this will work on CE, but the following works on the desktop (for a UNICODE build):

TRACE( TEXT("Unicode string: "%s", ASCII string: "%S""), L"unicode", "ascii");

It should work as long as the text retrieved is really ASCII in the 0–127 range, and the Unicode encoding is UTF-8. Unicode has adopted the lower ASCII range, using the same code points.





相关问题
Problem statically linking MFC libraries

I have a Visual Studio 6 workspace I m trying to convert to a Visual Studio 2008 solution. The output of said solution is a .dll. It has to be a .dll and it needs to statically link MFC as I can t ...

Switching to WPF. Is it time?

I m considering switching from MFC to WPF. My first concern is that there are too many users who don t have .NET with WPF installed yet. Can anybody point to a source containing the WPF penetration ...

Crash within CString

I am observing a crash within my application and the call stack shows below mfc42u!CString::AllocBeforeWrite+5 mfc42u!CString::operator=+22 No idea why this occuring. This does not occur ...

C# for UI, c++ for library

I have a numerical library coded in C++. I am going to make a UI for the library. I know some MFC. So one solution is to use MFC and make a native application. The alternative is C#. I know nothing ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

MFC List Control

In MFC, I can edit the text of items in the list control but only for the first column by setting the Edit Labels to true. Now when I click the first column item to change its text, I m able to change ...

热门标签