简体   繁体   English

我应该使用wsprintf()将double打印为宽字符串吗?

[英]Should I use wsprintf() to print a double as a wide string?

I am unable to print double value using wsprintf() . 我无法使用wsprintf()打印double值。 I tried sprintf() and it worked fine. 我试过sprintf() ,它工作正常。

Syntax used for wsprintf() and sprintf() is as follows: 用于wsprintf()sprintf()语法如下:

wsprintf(str,TEXT("Square is %lf "),iSquare); // Does not show value

sprintf(str," square is %lf",iSquare);  // works okay

Am I making any mistakes while using wsprintf() ? 我在使用wsprintf()wsprintf()

wsprintf doesn't support floating point. wsprintf不支持浮点。 The mistake is using it at all. 错误就是使用它。

If you want something like sprintf , but for wide characters/strings, you want swprintf instead. 如果你想要像sprintf这样的东西,但对于宽字符/字符串,你需要swprintf

Actually, since you're using the TEXT macro, you probably want _stprintf instead though: it'll shift from a narrow to wide implementation in sync with the same preprocessor macros as TEXT uses to decide whether the string will be narrow or wide. 实际上,由于你正在使用TEXT宏,你可能会想要_stprintf :它将从窄到宽的实现转换为与TEXT用来决定字符串是窄还是宽的相同的预处理器宏。 This whole approach, however, is largely a relic from the days when Microsoft still sold and supported versions of Windows based on both the 32-bit NT kernel, and on the 16-bit kernel. 然而,整个方法在很大程度上是微软仍然出售和支持基于32位NT内核和16位内核的Windows版本的时代遗留下来的。 The 16-bit versions had only extremely minimal wide-character support, so Microsoft worked hard at allowing a single source code base to be compiled to use either narrow characters (targeting 16-bit kernels) or wide characters (to target the 32-bit kernels). 16位版本只有极少的宽字符支持,因此Microsoft努力允许编译单个源代码库以使用窄字符(以16位内核为目标)或宽字符(以32位为目标)内核)。 The 16-bit kernels have been gone for long enough that almost nobody really has much reason to support them any more. 16位内核已经消失了很长时间,几乎没有人真正有理由再支持它们了。

For what it's worth: wsprintf is almost entirely a historic relic. 值得一wsprintf是: wsprintf几乎完全是一个历史遗迹。 The w apparently stands for Windows . w显然代表Windows It was included as part of Windows way back when (back to the 16-bit days). 它被包含在Windows的一部分中(当时回到16位日)。 It was written without support for floating point because at that time, Windows didn't use any floating point internally--this is part of why it has routines like MulDiv built-in, even though doing (roughly) the same with floating point is quite trivial. 它是在没有浮点支持的情况下编写的,因为当时Windows并没有在内部使用任何浮点数 - 这就是为什么它有像MulDiv内置的例程的一部分,即使用浮点数做(大致)相同也是如此。很琐碎。

wsprintf does not support floating point. wsprintf不支持浮点。 See its documentation - lf is not listed as a valid format code. 请参阅其文档 - lf未列为有效的格式代码。

The swprintf function part of the Visual Studio standard library is what you want. Visual Studio标准库的swprintf函数部分是您想要的。 It supports all of the format codes that sprintf does. 它支持sprintf执行的所有格式代码。

The function wsprintf() does not support floating point parameters, try using swprintf() instead if you're working with floating point values. 函数wsprintf()不支持浮点参数,如果您正在使用浮点值,请尝试使用swprintf()

More information about swprint can be found here 有关swprint的更多信息,请访问此处

大概你没有编译成UNICODE而TEXT只是#defined而只是一个常规字符串。

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

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