c - 在 C 中打印 unicode 字符串

标签 c windows unicode printf

我有一个包含以下句子的文件:returnCodeMsgDE=Es gibt nicht genug Arbeitsspeicher um das Programm auszuf\u00FChren。 Zurzeit gibt es %d frei MB zu verf\u00FCgung

我想读取这些句子,将它们写入一个将传递给 MessageBox 的变量,这样我就可以将它们写入对话框窗口,其中 unicode 符号应替换为与 unicode 等效的本地字符。

但是我得到的对话框带有这样的文本:“auszuf\u00FChren”。

这是我的代码中发生这种情况的部分:

_TCHAR*   errorMsg = NULL;

_TCHAR* returnCodeMsgDE2 = readPropertiesFile(_T_ECLIPSE("returnDE")); //here I get this string: L"returnCodeMsgDE=Es gibt nicht genug Arbeitsspeicher um das Programm auszuf\\u00FChren. Zurzeit gibt es %d frei MB zu verf\\u00FCgung"

_stprintf(errorMsg, _T_ECLIPSE("%s"), returnCodeMsgDE2, _freeMemory()); //freememory() returns an Integer.


MessageBox( topWindow, errorMsg, title, MB_OK );

我不知道如何让我的程序正确表示 Unicode 符号,我尝试了很多 printf 和 scanf unicode 版本,但没有一个有效。

我也尝试过设置区域设置。

你能帮我吗?我此刻完全迷失了。

最佳答案

您收到的字符串是L"auszuf\\u00FChren",以 C 源表示法编写,结果为“auszuf\u00FChren”。

您将需要一个额外的转义层,以便将序列 \u00FC 转换为元音变音 ü。下面的示例为 C 中的 UNICODE 和 ASCII 构建的 C 字符串语法实现了(非常粗略的)转义机制:

#include <windows.h>

#ifdef UNICODE
#define stprintf swprintf
#else
#define stprintf sprintf
#endif

static int hexdigit(TCHAR c)
{
    if ('0' <= c && c <= '9') return c - '0';
    if ('a' <= c && c <= 'f') return c - 'a' + 10;
    if ('A' <= c && c <= 'F') return c - 'A' + 10;
    return -1;
}

static TCHAR hexcode(TCHAR const **p, int n)
{
    TCHAR uc = 0;

    while (n--) {
        int d = hexdigit(*(*p)++);

        if (d < 0) return 0xfffd;
        uc = (uc << 4) + d;
    }
    return uc;
}

/*
 *      Resolve C escapes in src and write up to n - 1 characters 
 *      to str, which is zero-terminated. Returns number of 
 *      characters in str, not counting the trailing NUL.
 */
int unescape(TCHAR *str, int n, const TCHAR *src)
{
    TCHAR *p = str;
    TCHAR *end = p + n - 1;

    while (*src) {
        if (p == end) break;

        if (*src == '\\') {
            src++;

            switch (*src++) {
            case 'n':   *p++ = '\n'; break;
            case 't':   *p++ = '\t'; break;
            case '\\':  *p++ = '\\'; break;
            case '\'':  *p++ = '\''; break;
            case '"':   *p++ = '\"'; break;
            case '0':   *p++ = '\0'; break;            
            case 'x':   *p++ = hexcode(&src, 2); break;                         
            case 'u':   *p++ = hexcode(&src, 4); break;

            /* Ignore octal notation and non-printable chars */
            }
        } else {
            *p++ = *src++;
        }
    }

    *p = '\0';
    return p - str;
}

#define MAXBUF 80

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    TCHAR *title_esc = TEXT("Speicherplatz ungen\\u00FCgend");
    TCHAR *fmt_esc = TEXT("Es stehen nur %d MB zur Verf\\u00FCgung!");
    TCHAR title[MAXBUF];
    TCHAR fmt[MAXBUF];
    TCHAR msg[MAXBUF];

    unescape(title, MAXBUF, title_esc);
    unescape(fmt, MAXBUF, fmt_esc);
    stprintf(msg, fmt, 17);

    MessageBox(NULL, msg, title, MB_OK);
    return 0;
}

可能已经有一个更清晰、更好实现的 API 函数,但我找不到它。

关于c - 在 C 中打印 unicode 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19904431/

相关文章:

windows - 尝试使用 PowerShell 2.0 将 regfile 复制到另一个文件夹

c++ - 'std::wstring_convert' 尽可能多地转换(从 UTF8 文件读取 block )

Python 检查 unicode 文件

c - 如何将相同的值写入 x86 中的连续位置

c - scanf返回值不符合预期

无法在 sqlite 中找到实际结构

linux - 虚拟机网络困境

c++ - 为所有用户设置注册表值

c - 在 VB.net 中加载 C DLL 的 EntryPointNotFoundException

java - 在 Java 正则表达式中匹配 Unicode 破折号?