【发布时间】:2015-12-18 13:35:09
【问题描述】:
尝试编写一个函数来动态编码和解码文本。但是,当我尝试从 Multibyte 转换为 Widechar 时遇到了一些问题。有谁知道我做错了什么?
BOOL decode_comp = TRUE;
wchar_t decode_key[] = L"abc123";
wchar_t myMessage[] = L"\x18\x35\x3C\x3C\x3F\x70\x07\x3F\x22\x3C\x34\x71\x00"; // Hello World
wchar_t *Decode(wchar_t *s)
{
unsigned int i, j;
wchar_t *string;
string = (wchar_t *)malloc((wcslen(s) + 1) * sizeof(wchar_t));
wcscpy(string, s);
for (i = 0; i < wcslen(string); i++)
{
for (j = 0; j < sizeof(decode_key); j++)
string[i] ^= decode_key[j];
if (decode_comp)
string[i] = ~string[i];
}
return string;
}
【问题讨论】:
-
这会返回什么:
sizeof(decode_key);? -
您能否解释一下究竟发生了什么与您的预期不同? (这与多字节字符串有什么关系?)
-
另外,选择一种语言,C 或 C++。如果使用 C++ 执行此操作,则只需使用两个算法函数(
transform、for_each)。
标签: c++ c string unicode encode