为什么不printf格式UNI code参数? [英] Why doesn't printf format unicode parameters?

查看:91
本文介绍了为什么不printf格式UNI code参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用printf格式化双字节字符串转换成单字节字符串:

When using printf to format a double-byte string into a single-byte string:

printf("%ls\n", L"s:\\яшертыHello");   // %ls for a wide string (%s varies meaning depending on the project's unicode settings).

psented为ASCII字符显然,某些字符不能重新$ P $,所以有时我看到那里的行为双字节字符都被转换为一个?标记字符。但是,这似乎取决于特定的字符。对于上述的printf,输出是:

Clearly, some characters can't be represented as ascii characters, so sometimes I have seen behaviour where double-byte characters get turned into a '?' mark character. But, this seems to depend on the particular characters. For the printf above, the output is:

s:\

我希望我可能会是这样的:

I was hoping I might get something like:

s:\??????Hello

我恐怕已经失去了的例子,但我认为一个字符串时,它遇到了单向code字,取而代之的是第一个用?然后放弃了休息。

I'm afraid I've lost the example, but I think for one string when it encountered unicode characters, replaced the first one with a '?' and then gave up on the rest.

所以,我的问题是,什么是应该当你格式化宽字符串为单字节字符串发生。文档在这里: http://msdn.microsoft.com/en-us/library/ hf4y5e3w.aspx 说:字符显示到第一个空字符。但是,我没有看到这一点。这是printf的一个bug,或者说是我看到的地方记录的行为,如果有,在哪里。

So, my question is, what's supposed to happen when you format a wide string into a single-byte string. Documentation here: http://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx says "Characters are displayed up to the first null character". But, I'm not seeing that. Is this a bug in printf, or is the behaviour I'm seeing documented somewhere, if so, where.

感谢您的帮助。

更新

感谢来自人民给我的替代品用printf答案。我要变成另一种选择,但我真正感兴趣的是出于好奇为什么的printf没有可靠记录的行为。看起来几乎一样,如果它的实施者走出自己的方式,使这个不行。

Thanks for the answers from people giving me alternatives to using printf. I am going to change to an alternative, but I'm really interested out of curiosity why does printf not have reliable documented behaviour. It appears almost as if the implementer of it went out of their way to make this not work.

推荐答案

我期待你的code的工作 - 它在这里工作在Linux上 - 但它依赖于语言环境。这意味着你必须设置的语言环境和你的语言环境必须支持所使用的字符集。下面是我的测试程序:

I expect your code to work -- and it works here on Linux -- but it is locale dependent. That means you have to set up the locale and your locale must support the character set used. Here is my test program:

#include <locale.h>
#include <stdio.h>

int main()
{
    int c;
    char* l = setlocale(LC_ALL, "");
    if (l == NULL) {
        printf("Locale not set\n");
    } else {
        printf("Locale set to %s\n", l);
    }
    printf("%ls\n", L"s:\\яшертыHello");
    return 0;
}

和这里是一个执行跟踪:

and here is an execution trace:

$ env LC_ALL=en_US.utf8 ./a.out
Locale set to en_US.utf8
s:\яшертыHello

如果它说,该区域未设置或设置为C,这是正常的,你没有得到你所期望的结果。

If it says that the locale isn't set or is set to "C", it is normal that you don't get the result you expect.

编辑:看答案<一个href=\"http://stackoverflow.com/questions/4324542/what-is-the-windows-equivalent-for-en-us-utf-8-locale\">this问题为en_US.utf8相当于为Windows。

see the answers to this question for the equivalent of en_US.utf8 for Windows.

这篇关于为什么不printf格式UNI code参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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