的swprintf扼流圈外的8位字符范围 [英] swprintf chokes on characters outside 8-bit range

查看:240
本文介绍了的swprintf扼流圈外的8位字符范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这发生在OS X,但我怀疑它适用于任何UNIX-Y操作系统。
我有两个字符串看起来像这样的:

This happens on OS X, though I suspect it applies to any UNIX-y OS. I have two strings that look like this:


const wchar_t *test1 = (const wchar_t *)"\x44\x00\x00\x00\x73\x00\x00\x00\x00\x00\x00\x00";
const wchar_t *test2 = (const wchar_t *)"\x44\x00\x00\x00\x19\x20\x00\x00\x73\x00\x00\x00\x00\x00\x00\x00";

在调试器,test1的样子DS和TEST2看起来像德公司(同花撇号)。然后我把这个code:

In the debugger, test1 looks like "Ds" and test2 looks like "D's" (with the curly apostrophe). I then call this code:


wchar_t buf1[100], buf2[100];
int ret1 = swprintf(buf1, 100, L"%ls", test1);
int ret2 = swprintf(buf2, 100, L"%ls", test2);

第一的swprintf调用工作正常。第二个返回-1(和缓冲是不变)。

The first swprintf call works fine. The second one returns -1 (and the buffer is unchanged).

我猜问题是与语言环境,但周围的Googling并没有给我提供任何有用的。这是重现我看到这个问题的最简单方法。什么我真正感兴趣的是vswprintf(),但我认为这是密切相关的。

I'm guessing the problem has something to do with locales but googling around didn't provide me with anything useful. This is the simplest way to reproduce the problem I'm seeing. What I'm really interested in is vswprintf(), but I assume that's closely related.

为什么的swprintf呛即8位范围之外的UNI code字?
反正是有解决此问题?

Why does swprintf choke on the unicode character that is outside of the 8-bit range? Is there anyway to work around this?

推荐答案

尝试明确设置区域设置为UTF-8。

Try explicitly set the locale to UTF-8.

setlocale(LC_CTYPE, "UTF-8");
...
const wchar_t* test2 = L"D\x2019s";
int ret2 = swprintf(buf2, 100, L"%ls", test2);
...

这篇关于的swprintf扼流圈外的8位字符范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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