Sign in to follow this  

Getting Japanese symbols and dont know why

This topic is 3412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to learn how to program using VC++ express 2008 and get into game design. I am running a simple program from a book that I bought and I am getting a strange output. I am not getting any error but just a strange output. If anyone can figure this out it would be great! Basically I am getting japanese symbols (garabage values i think) instead of the words "hello world!". Code is: /* Beginning Game Programming * Chapter 3 * WindowTest program */ // header files to include #include <windows.h> #include <stdlib.h> #include <time.h> // application title #define APPTITLE "HelloWorld" // function prototypes(forward declartion) BOOL InitInstance(HINSTANCE, int); ATOM MyRegisterClass(HINSTANCE); LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM); // the window event callback function LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; char *szHello = "HelloWorld!"; RECT rt; int x, y, n; COLORREF c; switch(message) { case WM_PAINT: // get the dimensions of the window GetClientRect(hWnd, &rt); // start drawing on device context hdc = BeginPaint(hWnd, &ps); // draw some text DrawText(hdc, (LPCTSTR)szHello, strlen(szHello), &rt, DT_CENTER); // draw 1000 random pixles for(n=0;n<3000;n++) { x = rand()%(rt.right - rt.left); y = rand()%(rt.bottom - rt.top); c = RGB(rand()%256, rand()%256, rand()%256); SetPixel(hdc, x, y, c); } // stop drawing EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hWnd, message, wParam, lParam); } // helper function to set up the window properties ATOM MyRegisterClass(HINSTANCE hInstance) { // create the window class structure WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); // fill the struct with info wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WinProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = (const TCHAR*)APPTITLE; wc.hIconSm = NULL; // set up the window with the class info return RegisterClassEx(&wc); } // helper function to create the window and refresh it BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; // create a new window hWnd = CreateWindow((const TCHAR*)APPTITLE, // window class (const TCHAR*)APPTITLE, // title bar WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // x position of window CW_USEDEFAULT, // y position of window 500, // width of the window 400, // height of the window NULL, // parent window NULL, // menu hInstance, // application instance NULL); // window parameters // was there an error creating the window? if(!hWnd) return FALSE; // display window ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // entry point for a window program int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // declare variables MSG msg; // register class MyRegisterClass(hInstance); // initialize application if(!InitInstance(hInstance, nCmdShow)) return FALSE; // set random number seed srand(time(NULL)); // main message loop while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }

Share this post


Link to post
Share on other sites
Change
char *szHello = "HelloWorld!";

to
LPCTSTR szHello = "HelloWorld!"; // LPCWSTR in recent versions of MSVC++


You can't directly typecast from a non-wide string to a wide string. Alternatively, if you don't care about unicode support you can change your call from DrawText to DrawTextA, or disable the DrawText macro from defining DrawTextW in your project properties.

Share this post


Link to post
Share on other sites
I changed the DrawText to DrawTextA(...) and that switched the lettering inside the window to english letters. Thank you very much. I am still getting the window title in japanese letters, any suggestions?

Share this post


Link to post
Share on other sites
Found it! I changed the properties in my project to use multi-byte character set and all my letters are in english. Does anyone know why it was doing that or why I had to change that setting? I am just curious because I am pretty OCD about not knowing every little detail when it comes to programming. Thanks for all the help.

Share this post


Link to post
Share on other sites
If you don't care about Unicode support, you can switch the character set (Unicode is set by default since VC 7 afaik). To do this, go to Properties->Configuration Properties->General->Character Set, and set it to something else than Unicode.

You'll notice that if you go to the definition of DrawText, it should look something like this:
#ifdef UNICODE
#define DrawText DrawTextW
#else
#define DrawText DrawTextA
#endif // !UNICODE

In any case, you should definitely have been getting compiling warnings about typecasting (to wide-strings).

Share this post


Link to post
Share on other sites
Unicode is the default character set used in Win32 apps because it can support non-ansi letters, so applications are more 'portable' in the sense that they can use the extended characters provided by multibyte representations.

If you want to use unicode, there are a couple of macros that will do the conversion from ansi to unicode for you. You can use them like this:
_T("ANSI text")
L"ANSI text"
TEXT("ANSI text")


All of the above do the same thing - convert a const char* to a LPCWSTR for use in Win32 wide functions.

Share this post


Link to post
Share on other sites
Older versions of Visual studio used to default to ASCII/ANSI/UTF-8/Multi-byte character sets, all of which are generally stored in an array of bytes (8-bit). New versions of Visual Studio now default to some form of Unicode/UTF-16, which generally uses short-integers (16-bit) to store characters.

Your code was probably defining an 8-bit string, and then passing it to a function that was expecting a 16-bit string.

Share this post


Link to post
Share on other sites

This topic is 3412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this