如何通过UNI code字迭代和printf的用C打印在屏幕上? [英] How to iterate through unicode characters and print them on the screen with printf in C?

查看:215
本文介绍了如何通过UNI code字迭代和printf的用C打印在屏幕上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过所有的迭代(至少16位)UNI code字符并打印与C屏幕

I want to iterate through all (at least the 16 bit) unicode characters and print them on the screen with C.

我知道有相关的SO问题,但他们不解决的printf C中的问题,但是这是我想要的东西来实现的,如果以后有可能所有。我认为应该是可能的,也许一招,我不知道。

I know there are related questions on SO but they don't solve the problem with printf in C, but this is what I want to achieve, if it's possible after all. I think it should be possible maybe with a trick I'm not aware of.

因为我想用printf,我想到是这样的:

Since I want to use printf, I thought about something like this:

for (int i = 0x0000; i <= 0xffff; i++) {

    //then somehow increment the string
    char str[] = "\u25A1\n";
    printf("%s", str);

    char str[] = "\u25A2\n";
    printf("%s", str);

    char str[] = "\u25A3\n";
    printf("%s", str);

    ...

}

但它是一个有点问题递增UNI code code点,这里 \\ u25A1 。我知道这是不可能的本身,因为像 \\ u0000的某些字符是不可打印,编译器说,没有。但除此之外,我怎么可能从十六进制0000至FFFF递增,打印与字符的printf

But it's a bit of a problem to increment the unicode code point, here \u25A1. I'm aware it's not possible per se because some characters like \u0000 are not printable and the compiler says no. But apart from that, how could I increment from hexadecimal 0000 to ffff and print the character with printf.

推荐答案

如果在 __ STDC_ISO_10646 __ 宏定义,宽字符对应的Uni code codepoints 。因此,假设一个区域,可以重新present你有兴趣,你可以的printf()通过宽字符%LC字符格式转换:

If the __STDC_ISO_10646__ macro is defined, wide characters correspond to Unicode codepoints. So, assuming a locale that can represent the characters you are interested in, you can just printf() wide characters via the %lc format conversion:

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

#ifndef __STDC_ISO_10646__
#error "Oops, our wide chars are not Unicode codepoints, sorry!"
#endif
int main()
{
        int i;
        setlocale(LC_ALL, "");

        for (i = 0; i < 0xffff; i++) {
                printf("%x - %lc\n", i, i);
        }

        return 0;
}

这篇关于如何通过UNI code字迭代和printf的用C打印在屏幕上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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