简体   繁体   English

使用 wsprintf 将 int 转换为 wchar_t*

[英]Using wsprintf to convert int to wchar_t*

I'm trying to get a wchar_t* formatted with an int as a parameter.我试图获得一个wchar_t*格式的int作为参数。 I've Googled a lot but I've only ended up more confused.我在谷歌上搜索了很多,但最终却更加困惑。 So, consider this code:因此,请考虑以下代码:

int main(int argc, char** argv) {

   wchar_t buf[16];
   wsprintf(buf, L"%d", 5);
   wprintf(L"[%ls]\n", buf);

   system("pause");
   return 0;

};

Having assumed that wchar_t , wsprintf and wprintf are the wide character equivalents of char , sprintf and printf respectively, I expected the above to print [5] , but it prints garbage between [ and ] .假设wchar_twsprintfwprintf分别是charsprintfprintf的宽字符等价物,我预计上面会打印[5] ,但它会在[]之间打印垃圾。 What is the correct way to achieve the desired result?达到预期结果的正确方法是什么? And what am I misunderstanding here?我在这里误解了什么?

(I should clarify that portability is more important than security here, so I'd like to know a solution that uses this family of functions instead of safer vendor-specific extensions.) (我应该澄清一下,这里的可移植性比安全性更重要,所以我想知道一个使用这一系列函数而不是更安全的供应商特定扩展的解决方案。)

wsprintf() is a Windows-specific function, it's unavailable on Unixes. wsprintf()是 Windows 特定的函数,它在 Unix 上不可用。 What you want to achieve can be done in a more portable way (I have tried this slightly modified code snippet and it worked as expected):您想要实现的目标可以以更便携的方式完成(我已经尝试过这个稍微修改过的代码片段,它按预期工作):

#include <wchar.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    wchar_t buf[16];
    swprintf(buf, sizeof(buf) / sizeof(*buf), L"%d", 5);
    wprintf(L"[%ls]\n", buf);

    return 0;
}

Output:输出:

[5]

wsprintf() is one of Microsoft's hybrid functions. wsprintf()是微软的混合函数之一。 It has a different signature depending on whether or not _UNICODE is defined during preprocessing.它具有不同的签名,具体取决于在预处理期间是否定义了_UNICODE (What's happening under the hood, is there's an #ifdef that replaces wsprintf with wsprintfA or wsprintfW based on the nonexistence or existence of that symbol.) wsprintf发生的事情,是否有一个#ifdef根据该符号的不存在或存在将wsprintf替换为wsprintfAwsprintfW 。)

If _UNICODE is defined, it expects the buffer to be of wchar_t[] .如果定义了_UNICODE ,它期望缓冲区是wchar_t[]

If _UNICODE is not defined (usually the default), it expects the buffer to be char[] .如果_UNICODE未定义(通常是默认值),它期望缓冲区为char[] That's what's happening here.这就是这里发生的事情。 If you had warnings on, it would probably show a warning about incompatible pointer types.如果您有警告,它可能会显示有关不兼容指针类型的警告。 The garbage is because the 8-bit ascii that it's storing in buf is being interpreted as Unicode by wprintf() .垃圾是因为它存储在buf的 8 位 ascii 被wprintf()解释为 Unicode。

It appears to me that you're using an incorrect format specifier for your output.在我看来,您为输出使用了不正确的格式说明符。 I would suspect you would want我怀疑你会想要

wprintf(L"[%s]\n", buf);

%ls looks like an invalid combination of specifiers - l for a "long int" size prefix on the s "string" type specifier. %ls 看起来像是无效的说明符组合 - l 表示 s “string”类型说明符上的“long int”大小前缀。 Documentation I've found on wprintf indicates that it treats the 's' type specifier as pointing to a wide character string (at least for MS Visual Studio).我在 wprintf 上找到的文档表明它将 's' 类型说明符视为指向宽字符串(至少对于 MS Visual Studio)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM