我正在使用std :: string作为文本缓冲区。然后,我确定该缓冲区中包含的数据是UTF-16(即它实际上是std :: wstring)。如何将std :: string强制转换为std :: wstring?std :: string是错误的名称,数据实际上是wstring。
我必须"翻译"一个:
char数据[32]
进入:
tstring数据;
其中tstringtypedef为std::string或std::wstring取决于预处理器定义.
假设data[32]只包含ASCII字符,那么将这些数据传递给tstring对象的最简单方法是什么?
我有
std::string str = "hello world";
std::wstring wstr = L"goodbye world";
std::cout << str << std::endl;
std::wcout << wstr << std::endl;
Run Code Online (Sandbox Code Playgroud)
当我在VS 6.0的调试器"自动"窗口中启动调试并在第三行设置断点时,我得到了:
std::cout {...}
str {0x001f2de1 "hello world"}
wstr {0x001f2e4a}
Run Code Online (Sandbox Code Playgroud)
而且我看不到wstr的价值......
我怎么能配置VS能够这个?
感谢名单
我需要为基于锌的 Flash 应用程序重构 .dll。
将一个类从 master 复制并粘贴到分支后,我遇到了一些奇怪的编译错误:
GameInfo.h(15): error C2146: syntax error : missing ';' before identifier 'm_wsVersion'
GameInfo.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
GameInfo.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Run Code Online (Sandbox Code Playgroud)
寻址代码:
// removed the comments
#include "stdafx.h"
#include <string.h>
class GameInfo {
public:
UINT m_uiGameId;
wstring m_wsVersion; // Line 15
UINT m_uiCheckSum;
wstring m_wsFilePath; // Same error report as on line 15 …Run Code Online (Sandbox Code Playgroud) 我试图为wstring指针赋值,并且没有赋值; 但是,当我打破创建并将指针分配到两行时,它可以工作.为什么是这样?
例如:
std::wstring* myString = &(L"my basic sentence" + some_wstring_var + L"\r\n");
Run Code Online (Sandbox Code Playgroud)
以上不起作用,但下面的确有效:
std::wstring temp = (L"my basic sentence" + some_wstring_var + L"\r\n");
std::wstring* myString = &temp;
Run Code Online (Sandbox Code Playgroud) 我想要一个'降低'功能(从单词)到两种语言正常工作,例如,英语和俄语.我该怎么办?我应该使用std :: wstring吗,或者我可以使用std :: string吗?此外,我希望它是跨平台的,不要重新发明轮子.
考虑这个将窄字符串转换为宽字符串的函数:
std::wstring convert(const std::string& input)
{
try
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(input);
}
catch(std::range_error& e)
{
std::size_t length = input.length();
std::wstring result;
result.reserve(length);
for(std::size_t i = 0; i < length; i++)
{
result.push_back(input[i] & 0xFF);
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
我很难理解在后备路径中需要这个表达式:
result.push_back(input[i] & 0xFF);
Run Code Online (Sandbox Code Playgroud)
为什么字符串中的每个字符都被 0xFF (0b11111111) 屏蔽?
我创建了这个最小的工作 C++ 示例代码片段来比较 astd::string和 a中的字节(通过它们的十六进制表示)在std::wstring定义一个带有德国非 ASCII 字符的字符串时。
#include <iostream>
#include <iomanip>
#include <string>
int main(int, char**) {
std::wstring wstr = L"äöüß";
std::string str = "äöüß";
for ( unsigned char c : str ) {
std::cout << std::setw(2) << std::setfill('0') << std::hex << static_cast<unsigned short>(c) << ' ';
}
std::cout << std::endl;
for ( wchar_t c : wstr ) {
std::cout << std::setw(4) << std::setfill('0') << std::hex << static_cast<unsigned short>(c) << ' ';
}
std::cout << std::endl; …Run Code Online (Sandbox Code Playgroud) 如何以编程方式查找 astd::wstring是否已分配Short String Optimization?我试图检测此类情况并用于reserve移出 SSO。
下面的代码在地址上打印了一个小的差异:
#include <string>
#include <iostream>
int main ()
{
std::wstring str = L"H";
std::cout<<&str<<" "<<(void*)str.data()<<"\n";
}
Run Code Online (Sandbox Code Playgroud)
输出示例:
0x7ffc39465220 0x7ffc39465230
Run Code Online (Sandbox Code Playgroud)
尽管在 Windows 控制台应用程序中,地址完全相同:
我有以下代码:
inline bool match(const std::wstring & text1, const std::wstring & text2)
{
return match(text1.c_str(), text2.c_str());
}
inline bool match(const std::wstring & text1, const wchar_t * text2)
{
return match(text1.c_str(), text2);
}
inline bool match(const wchar_t * text1, const std::wstring & text2)
{
return match(text1, text2.c_str());
}
inline bool match(const wchar_t * text1, const wchar_t * text2)
{
return !wcscmp(text1, text2);
}
Run Code Online (Sandbox Code Playgroud)
我得到:
error C2666: 'match' : 3 overloads have similar conversions
1> could be 'bool match(const wchar_t *,const std::wstring &)' …Run Code Online (Sandbox Code Playgroud)