I'm writing an Excel plugin, and need to generate wchar_t
output for Excel (although internally, we are 100% char
, and
in fact limit char
to plain ASCII). At one point, I'm using
swprintf
to do the conversion:
static wchar_t buffer[ 32369 ];
buffer[0] = swprintf( buffer + 1, sizeof(buffer) - 1, L"#%s!", message );
Excel displays some sort of CJK characters, although message
(type char const*
) is a null terminated character string with
no characters outside of printable ASCII (hex values 0x20-0x7E).
I've tried this in a small test program, dumping in hex the
generated string, and it looks like VC++ is treating message
as if it were a wchar_t const*
(although it seems to
recognized the '\0'
correctly, although it is on a single
byte); this results in wchar_t
with values like 0x6568
(rather than the 0x0068, 0x0065
that I was expecting).
According to the C99 standard, for a "%s"
specifier,
swprintf
should convert the characters from the char const*
"as if by repeated calls to the mbrtowc
function[...]". Is
the behavior I am seeing is an error in the Visual C++ library,
or whether there is something in the global locale that I have
to change?
(FWIW: when I compile and run my small test program with g++, I get the behavior I expect. G++ is not, however, an option for our Excel plugins, at least not at present.)
Note that from swprintf of MSDN:
and then in the example:
so at least Microsoft documented this.
And then in the page of format specifiers
And then
So what you want is upper-case
%S
.See even this similar question: visual studio swprintf is making all my %s formatters want wchar_t * instead of char * where they suggest using
%ls
(always consider the parameterwchar_t*
) and%hs
(always consider the parameterchar*
)