简体   繁体   English

C ++将字符串转换为wchar_t

[英]C++ Converting string to wchar_t

I just started using GDI+ and there will be quite a few conversions. 我刚刚开始使用GDI +,并且会有很多转换。

The text I draw appears for a split second as a bunch of random symbols. 我绘制的文本瞬间显示为一堆随机符号。 I traced the problem down to the conversions. 我将问题归结为转换。

Code updated at bottom 代码在底部更新

const wchar_t* convert::stringToWideChar(string String) {

  wstring wideString;

  for(int i = 0; i < String.length(); i++)
      wideString += wchar_t( String[i] );

  return wideString.c_str();

}

Used as: 用作:

testing = "Two Two";
const wchar_t* buf = convert::stringToWideChar(testing);
path.AddString(buf, wcslen(buf), &gFontFamily, FontStyleBold, 72, ptOrg, &strFormat);

图片

The background is how the text is initially drawn. 背景是最初绘制文本的方式。 "Level 0" is what should be initially drawn. 首先应绘制“ 0级”。 (In the screenshot the initial text is faded as should.) (在屏幕快照中,初始文本会逐渐消失。)

Any ideas to do the conversion faster? 有什么想法可以加快转换速度吗? (The strangest thing is it only happens with random specific strings, ie "Two" works, "Two Two", doesn't, "Two Too", does. (最奇怪的是,它仅在随机的特定字符串中发生,例如,“ Two”有效,“ Two Two”有效,“ Two Too”有效。

Updated 更新

wstring convert::stringToWideChar(string String) {
wstring wideString;

for(int i = 0; i < String.length(); i++)
    wideString += wchar_t(String[i] );

return wideString;
}

Used as: 用作:

const wchar_t* buf = convert::stringToWideChar("leveljjj").c_str();

Re: your updated code; 回复:您更新的代码;

There is a difference in lifetime between an automatic variable and a temporary. 自动变量和临时变量之间的生存期有所不同。

An automatic variable is declared in the function and will persist until the end of the scope. 在函数中声明了一个自动变量,该变量将一直持续到作用域结束。

A temporary will only persist until the end of the expression of which it is a part. 临时项将一直持续到它所包含的表达式的结尾。

An example; 一个例子;

testing = "Two Two";
const wchar_t* buf = convert::stringToWideChar("leveljjj").c_str();
path.AddString(buf, wcslen(buf), &gFontFamily, FontStyleBold, 72, ptOrg, &strFormat);

In this case the string returned from stringToWideChar is a temporary. 在这种情况下,从stringToWideChar返回的字符串是临时的。 It only persists until the expression of which it is a part is complete, that is until the assignment line is finished. 它仅会持续到它所属于的表达式完成为止,也就是直到赋值行完成为止。 It will be destroyed before the path.AddString line. 它会在path.AddString行之前被销毁。

If you on the other hand did this; 另一方面,如果您这样做了;

testing = "Two Two";
std::wstring value = convert::stringToWideChar("leveljjj");
const wchar_t* buf = value.c_str();
path.AddString(buf, wcslen(buf), &gFontFamily, FontStyleBold, 72, ptOrg, &strFormat);

...value is an automatic variable, which persists until the end of the scope. ... value是一个自动变量,将一直保留到作用域末尾。 That means, it's safe to use the buffer returned since the variable is still valid for the AddString line. 这意味着,使用返回的缓冲区是安全的,因为该变量对于AddString行仍然有效。

In your updated code your c_str() call will return a pointer into to a temporary object (rvalue) which is destroyed at the semicolon. 在更新的代码中,您的c_str()调用将返回一个指向临时对象(右值)的指针,该指针在分号处被破坏。

You'd need something like this: 您需要这样的东西:

void stringToWideChar(string String, char* destination) {
    wstring wideString;

    for(int i = 0; i < String.length(); i++)
        wideString += wchar_t(String[i] );

    memcpy(destination, wideString.c_str(), strlen(wideString.c_str));
}

Make sure to preallocate your buf, fill it with 0 (to get 0 termination), etc. 确保预先分配您的buf,将其填充为0(以得到0终止),等等。

如果更改转换函数以返回wstring,则将删除代码中的悬挂引用。

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

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