3

我有遗留代码,我正在逐步移植到 Visual C++ (wchar_t) 中的 Unicode 字符。我遇到了我想转换的这段代码:

char tmp[256];
sprintf(tmp, "stuff");
throw exception(tmp);

我想把它改成这样(这给了我一个异常的编译错误):

wchar_t tmp[256];
swprintf(tmp, "stuff");
throw exception(tmp);

到目前为止,我还没有找到任何文件给我抛出异常的 Unicode 等效项,有人可以帮助我吗?

当然,我可以将我的“tmp”转换回 char 字符串,但这样做似乎很愚蠢。

4

1 回答 1

3

std::exception不支持wchar_t字符串,因此您必须将wchar_t缓冲区转换为单独的char缓冲区,或者不要切换到wchar_t缓冲区开始,因为支持通过其和格式化说明符sprintf()格式化 Unicode 输入,例如:%S%ls

char tmp[256]; 
sprintf(tmp, "%ls", wchar_t data here); 
throw exception(tmp); 
于 2011-12-08T20:43:57.447 回答