了解哪些的swprintf将被用于(或再次转换成一个char *字符串为wchar_t *) [英] Understanding which swprintf will be used (or again, convert a char* string to wchar_t*)

查看:464
本文介绍了了解哪些的swprintf将被用于(或再次转换成一个char *字符串为wchar_t *)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换一个char *字符串为wchar_t *。我看到这个问题已经被问了很多次,没有解决/便携式答案/解决方案。

由于这里建议,似乎标准的swprintf正确的解决方案给我,但我发现存在两个版本有! !即:


  1. http://www.cplusplus.com/reference/cwchar/swprintf/(第二个参数是字符串容量)

  2. http://msdn.microsoft。 COM / EN-US /库/ ybk95axf%28V = vs.71%29.aspx (第二个参数是已经格式化字符串)

我的计划是这样的:

  const的无符号整型LOCAL_SIZE = 256;
字符* myCharString =世界,你好!
wchar_t的myWCharString [LOCAL_SIZE]

而在这一点:

 的swprintf(myWCharString,LOCAL_SIZE,L%HS,myCharString);

 的swprintf(myWCharString,L%HS,myCharString);

和切换编译器(MinGW的4.5.2< - > MinGW的4.7.2)我在一种情况下,在编译时错误确实得到了不同版本的顺利实施,那么!
我的问题:


  1. 有没有办法知道我在编译时选择其中2接口?

  2. 是否有替代品,便携办法在wchar_t改造一个char *字符串*?我可以通过C ++的std库(没有C ++ 11),例如,如果有必要

修改

的std :: wstring_convert 似乎并没有可用于我的编译器(4.5.2都不4.7.2也不包括的#include&LT ;区域>

我将在后面看看,如果我可以使用升压格式库来尝试解决这个...


解决方案

既然我可以使用C ++和效率是不是一个问题,我可以使用以下命令:

 的std :: wstring的(myCharString,myCharString +的strlen(myCharString))。c_str()

如果投入 wchar_t的* 是必要的,也可能是这样的:

<$p$p><$c$c>strcpy(myWCharString,std::wstring(myCharString,myCharString+strlen(myCharString)).c_str() );



经测试的 这里

basic_string的构造方法文档:

 第一,最后
    输入迭代器在一个范围内的最初和最终位置。
    使用的范围是[第一,去年),其中包括所有的字符
    第一个和最后,包括第一,但没有指出字符之间
    字符由最后指出。
    函数模板参数InputIterator的应是一个输入迭代器类型
    指向一个类型转换为图表元素。
    如果InputIterator为一个整数类型,参数铸造到
    适当的类型,以便签名(5)代替。

I am trying to convert a char* string to wchar_t*. I have seen this question has been asked many times, with no resolving/portable answer/solution.

As suggested here, swprintf seemed the right solution to me, but I found out there exist two versions out there!! Namely:

  1. http://www.cplusplus.com/reference/cwchar/swprintf/ (second argument is the string capacity)
  2. http://msdn.microsoft.com/en-us/library/ybk95axf%28v=vs.71%29.aspx (second argument is already the format string)

My program would look like this:

const unsigned int LOCAL_SIZE = 256;
char* myCharString = "Hello world!";
wchar_t myWCharString[LOCAL_SIZE];

And at this point:

swprintf(myWCharString,LOCAL_SIZE,L"%hs",myCharString );

or:

swprintf(myWCharString,L"%hs",myCharString );

And switching compiler (mingw 4.5.2 <-> mingw 4.7.2) I did get that different version were implemented, so in one case an error at compilation time! My questions:

  1. Is there a way to know which of the 2 interfaces I have to choose at compile time?
  2. Is there an alternative, portable way to transform a char* string in a wchar_t*? I can pass through C++ std libraries (no C++11) for example if necessary

Edit

std::wstring_convert doesn't seem to be available for my compiler (neither 4.5.2 nor 4.7.2, including #include <locale>

I will check out later if I can use Boost Format Library to try to solve this...

解决方案

Since I can use C++, and efficiency is not an issue, I can use the following:

std::wstring(myCharString,myCharString+strlen(myCharString)).c_str()

And if putting in a wchar_t* was necessary, it could be like this:

strcpy(myWCharString,std::wstring(myCharString,myCharString+strlen(myCharString)).c_str() );


Tested here.

Documentation from basic_string constructor methods:

first, last
    Input iterators to the initial and final positions in a range. 
    The range used is [first,last), which includes all the characters
    between first and last, including the character pointed by first but not
    the character pointed by last.
    The function template argument InputIterator shall be an input iterator type
    that points to elements of a type convertible to charT.
    If InputIterator is an integral type, the arguments are casted to the
    proper types so that signature (5) is used instead.

这篇关于了解哪些的swprintf将被用于(或再次转换成一个char *字符串为wchar_t *)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆