| Patlegroin (12) | |||||||
|
Hi all, I got a problem trying to trace unicode string in a file. My trace class is ascii, not unicode. I don't really care : at tracing time, I just convert from unicode to ascii. Well... it seems not to work at all. Though, the code is not from me, it's from some forum, and it looked pretty good to me. The calling part is here :
The getText & narrow fonctions are here :
The text in the edit is "that's a test" The resulting in the file is :
The text in the edit is "FFFAAA" The resulting in the file is :
About the trace class, I've been using it for a long time, it's been working fine. So I guess the problem is before that, maybe during the conversion from unicode to 1 byte. I have no idea. Thank you for helping ! | |||||||
|
|
|||||||
| closed account (DSLq5Di1) | |||
#include <locale>
Adapted from the example on MSDN @ http://msdn.microsoft.com/en-us/library/145y2358 | |||
|
|
|||
| Patlegroin (12) | |
| Hm... it doesn't change anything, I get the same logs with the same text in the edit >< | |
|
|
|
| Patlegroin (12) | |
| Now I'm trying to change my trace class to wstring, maybe I should have started there. | |
|
|
|
| modoran (1245) | |
|
To convert from ANSI to UNICODE use MultiByteToWideChar() API or mbstowcs, _mbstowcs_l from CRT. http://msdn.microsoft.com/en-us/library/dd319072%28v=vs.85%29.aspx http://msdn.microsoft.com/en-us/library/k1f9b8cy%28v=vs.100%29.aspx You have code example on MSDN webpage linked. | |
|
|
|
| codekiddy (602) | |
|
I would recommend you to restructure your whole code so that it strictly uses WINAPI data types for the sake of simplicity and portability, all of API data types are defined in WinNT.h, WinDef.h and WinTsd.h | |
|
|
|
| closed account (DSLq5Di1) | |||
That may be best. I've had no luck reproducing the issue from the snippets you've provided. You should also be wary of using wide functions with TCHARs, as in CreateWindowExW( TEXT("Edit"), ... ), either call CreateWindowEx() and work with TCHARs, or use the wide function with a wide string literal L"Edit", don't mix and match.If you need any help with your trace class, feel free to post the details. | |||
|
|
|||
| Patlegroin (12) | |
|
@modoran : I first tried to convert unicode to ansi, now I'm trying to rewrite my class to manipulate wstring instead of string. I don't have need for ansi to unicode so far. Maybe I will, so I note that down. Thank you. @codekiddy : I'm quite confused with what is standard type, what is not. My goal is to be the most simple, but when I search some help on internet, sometimes I try to use method using exotic types... I will check the files you said and see what I can keep as types. Thank you. @sloppy9 : you're right, I'm now changing my string constants to TCHARS. I want to use unicode from basement to ceiling. Thank you. I quickly get confused with all the differents types having the same purposes(from my comprehension), like wchar_t, TCHAR, WCHAR, etc. | |
|
Last edited on
|
|
| modoran (1245) | |
|
Well, if you want to convert unicode to ansi use WideCharToMultiByte() or wcstombs from CRT. http://msdn.microsoft.com/en-us/library/dd374130%28v=vs.85%29.aspx http://msdn.microsoft.com/en-us/library/5d7tc9zw%28v=vs.100%29.aspx | |
|
|
|
| Patlegroin (12) | |||||
|
Thanks for helping. I tried wcstombs, I had no success with it, but I don't rememer what was wrong. Maybe I will make another try. And about WideCharToMultiByte()... it's just... wow...
8 parameters ? Sorry, but I'm used to atoi or itoa, at worst 3 parameters. That just seems crazy to me. Like breaking a nut with a drop hammer or something. I will try it if someone put a gun on my head. Now I'm trying to use unicode everywhere, instead of converting, if it's possible(I mean if every method exists in unicode form). I have problem compiling my class, cause there is a macro inside it. And it seem to be the problem. I don't know how to solve that kind of thing, since there is no type in a macro. Here is the trace.h file :
| |||||
|
|
|||||
| Patlegroin (12) | |||
Here is the trace.cpp file :
| |||
|
Last edited on
|
|||
| Patlegroin (12) | ||||
The calls look like TRACE(CTrace::NIV_DEB, _T("test"));The error is :
I wonder if all this would not be much more easier in Java ? | ||||
|
|
||||
| closed account (DSLq5Di1) | |||||||||
|
Trace.h :::: Line 18 #include There is a couple of std::strstream objects in your code, but they have long been deprecated in favour of std::stringstream, the appropriate header to include is <sstream>.:::: Line 24 & 25 http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80) :::: Line 26 Replace std::ostrstream with std::wstringstream.:::: Line 27 os << MESSAGE std::ends is an artifact of using std::strstream, remove all occurrences.:::: Line 29 Remove freeze(), same reason as above. Trace.cpp :::: Line 146 wss << counter Remove std::ends.:::: Line 204 & 205 (Forum: Line 49 & 50 in the latter half of trace.cpp)
_countof(tmpbuf).:::: Line 235 (Forum: Line 80 in the latter half of trace.cpp) Remove std::ends.That'll solve the compile and run-time errors I've come across. A couple of suggestions to further clean up your code:- :::: Lines 29 - 37 (assume I'm referring to the latter half of trace.cpp on the forums from now on) Hex output using a stream,
:::: Lines 49 - 59 There is typically a wide function for every winapi / c standard function you come across,
:::: Lines 110 - 133 stringCopyAvecEspaces() could be written as,
Note: I think you misunderstood my earlier post regarding mixing TCHAR with wide functions, don't use TCHARs, TEXT() or _T() now since you are focused on Unicode. wchar_t literals are prefixed with an L. wchar_t example[] = L"hello";.If you can, avoid using C stdio functions (eg. printf() and it's derivatives), prefer C++ streams, iostream/fstream/stringstream. | |||||||||
|
Last edited on
|
|||||||||