您收到的字符串是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 函数,但我找不到它。