在使用unicode方面需要帮助 [英] Need help in working with unicode

查看:123
本文介绍了在使用unicode方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我一直在学习本教程的unicode:

什么是TCHAR,WCHAR,LPSTR,LPWSTR,LPCTSTR(等)? [ ^ ]

偶然发现一些代码问题,我不知道如何解决。



我的问题是:

1)

Hi! I've been learning unicode from this tutorial:
What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?[^]
and stumbled upon some code problems which I have no idea how to fix.

My problems are :
1)

wchar_t ch2[] = L"black☺";
wcout << ch2;



没有任何反应。如果我删除笑脸然后wcout键入它。不是wcout应该能够输入unicode吗?怎么了?



2)


nothing happens. If I remove the smiley face then wcout types it. Isn't wcout supposed to be able to type unicode? What's up with that?

2)

wchar_t ch2[] = L"black☺";
wchar_t *cht = new wchar_t;
int size = wcslen(ch2);
wcscpy_s(cht, size+1, ch2);
wcout << cht << endl;



如果我编译这段代码,那么屏幕上只会打印出黑色部分,那么笑脸就不会出现,而endl代码甚至都没有工作(所以我不能结束,除非我删除笑脸);



3)


If I compile this code then only the "black" part is printed on the screen, then smiley face is not and the "endl" code doesn't even work (so I can't end line unless I remove the smiley face);

3)

wchar_t *te = new wchar_t;
wcin >> te;
wcout << te << endl;





无论我打字什么,输出总是一无所获...



我只是不明白......要么我做了一些完全错误的事情,要么c ++只是为了理解而打字unicode。请帮我弄清楚我的错误。



The output is always nothing no matter what I type...

I just don't get it... Either I'm doing something completely wrong or c++ just made typing unicode too hard for my understanding. Please help me figure out my mistakes.

推荐答案

例如见: Unicode输出到Windows控制台 [ ^ ]。


例如,不要依赖字符串中的笑脸字符来有正确的预期字符代码。尝试使用unicode escape \ u263a。换句话说,尝试

For one, don't rely on the smiley-character in your string to have the correct intended character code. Try to use the unicode escape \u263a instead. In other words, try
wchar_t ch2[] = L"black\u263a";



你的程序中的第二个错误是在行中


The second mistake in your program is in the line

wchar_t *cht = new wchar_t; // wrong!



您正在为一个宽字符分配空间。您可能想要的是:


You are allocating room for exactly one wide-character. What you probably intended is something like:

int size = wcslen(ch2);
wchar_t *cht = new wchar_t[size+1];
wcscpy_s(cht, size+1, ch2);
...


这篇关于在使用unicode方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆