如何使用UTF-8和UTF-16。 [英] How to use UTF-8 and UTF-16.

查看:110
本文介绍了如何使用UTF-8和UTF-16。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个程序的目的应该是这样我可以使用我喜欢的'å','ä'和'ö'字母:)。

我已经阅读了很多关于这个主题但是可以找不到如何使用和包含UTF-8字符串的任何例子(在C中)(让我们将这个问题集中在UTF-8上并稍后使用UTF-16)。

我找到的最好的是使用setlocale()函数。

但是如何只使用该函数?



The purpose of this program should be so I can use my fancy 'å', 'ä' and 'ö' letters :).
I've read a lot about the subject but can't find any examples (in C) of how to use and include UTF-8 strings (lets focus this question on UTF-8 and take UTF-16 later).
The best I've found is to use the setlocale() function.
But how do one simply use that function?

int _tmain(int argc, _TCHAR* argv[])
{
  setlocale( LC_ALL, "sv" );
  wchar_t UTF-8_test[] = {'å', 'ö', 'ä'};
  
 return 0;
}



这是我对如何测试它的第一直觉......

你会惊讶于它确实有效吗? UTF-8_test字符串的值不正确。


This was my first instinct on how to test it...
Would you be suprised that it did work but the values of the UTF-8_test string were incorrect.

推荐答案

wchar_t 类型是一个16位字符,即UTF16。您还需要在所有字符和字符串文字上使用 L 前缀,以便编译器生成正确的字符。因此,您的代码应如下所示:

The wchar_t type is a 16 bit character, i.e. UTF16. You also need to use the L prefix on all your character and string literals so the compiler generates the correct characters. So your code should look like:
int _tmain(int argc, _TCHAR* argv[])
{
  setlocale( LC_ALL, "sv" ); // no L here as function takes a char literal
  wchar_t UTF-16_test[] = {L'å', L'ö', L'ä'};
  
 return 0;
}


这篇关于如何使用UTF-8和UTF-16。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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