wstring

Why mask a char with 0xFF when converting narrow string to wide string?

≡放荡痞女 提交于 2019-12-11 06:34:34
问题 Consider this function to convert narrow strings to wide strings: 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; } } I am having difficulty understanding the need for this

Find length of std::wstring [closed]

萝らか妹 提交于 2019-12-10 22:40:01
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . How can I determine the length(number of characters) in a std::wstring ? Using myStr.length() gives the byte size(I think) but its not the number of

Find method in std::wstring

青春壹個敷衍的年華 提交于 2019-12-10 18:57:24
问题 I have declared Wstring as follows wstring strID When I try to find the occurrences sub-string as follows int index = strID.find("LABS"); I am getting error like the following error C2664: 'unsigned int std::basic_string<_Elem,_Traits,_Ax>::find(const std::basic_string<_Elem,_Traits,_Ax> &,unsigned int) const' : cannot convert parameter 1 from 'const char [13]' to 'const std::basic_string<_Elem,_Traits,_Ax> &' Can you please help me to find the occurrences of sub-string? 回答1: When searching a

How can I use wstring(s) in Linux APIs?

淺唱寂寞╮ 提交于 2019-12-10 15:49:19
问题 I want to develope an application in Linux. I want to use wstring beacuse my application should supports unicode and I don't want to use UTF-8 strings. In Windows OS, using wstring is easy. beacuse any ANSI API has a unicode form. for example there are two CreateProcess API, first API is CreateProcessA and second API is CreateProcessW. wstring app = L"C:\\test.exe"; CreateProcess ( app.c_str(), // EASY! .... ); But it seems working with wstring in Linux is complicated! for example there is an

How to convert wstring to wchar_t*? C++

有些话、适合烂在心里 提交于 2019-12-10 12:34:50
问题 I would like to convert wstring to wchar_t*. I have tried everything what i know, please help. I would like to convert wstring to wchar_t*. 回答1: Did you try reading the reference const wchar_t* wcs = s.c_str(); 回答2: There is no way to convert wstring to wchar_t* but you can convert it to const wchar_t* which is what answer by K.Kirsz says. This is by design because you can access a const pointer but you shouldn't manipulate the pointer. See a related question and its answers. The best bet is

How can I get the byte size of std::wstring?

徘徊边缘 提交于 2019-12-10 01:36:31
问题 I am using std::wstring as my Unicode style string. Now I want to get the byte size of a wstring . If I use size() method of wstring , I just get the total number of chars in my wstring . But the byte should be size() * 2. Is there an official way to get this byte size? I don't want to use size() * 2 in my program..... I want to use in RegSetValueExW as last parameter. 回答1: Use str.size() * sizeof(wchar_t) or equivalent. In fact, I can't think of any other platform besides Windows that has 2

How to print wstring on Linux/OS X?

别等时光非礼了梦想. 提交于 2019-12-09 13:05:10
问题 How can I print a string like this: €áa¢cée£ on the console/screen? I tried this: #include <iostream> #include <string> using namespace std; wstring wStr = L"€áa¢cée£"; int main (void) { wcout << wStr << " : " << wStr.length() << endl; return 0; } which is not working. Even confusing, if I remove € from the string, the print out comes like this: ?a?c?e? : 7 but with € in the string, nothing gets printed after the € character. If I write the same code in python: #!/usr/bin/env python # -*-

problem using getline with a unicode file

非 Y 不嫁゛ 提交于 2019-12-07 19:31:14
问题 UPDATE: Thank you to @Potatoswatter and @Jonathan Leffler for comments - rather embarrassingly I was caught out by the debugger tool tip not showing the value of a wstring correctly - however it still isn't quite working for me and I have updated the question below: If I have a small multibyte file I want to read into a string I use the following trick - I use getline with a delimeter of '\0' e.g. std::string contents_utf8; std::ifstream inf1("utf8.txt"); getline(inf1, contents_utf8, '\0');

why std::wofstream do not print all wstring into file?

折月煮酒 提交于 2019-12-07 17:38:52
问题 I have a std::wstring whose size is 139,580,199 characters. For debugging I printed it into file with this code: std::wofstream f(L"C:\\some file.txt"); f << buffer; f.close(); After that noticed that the end of string is missing. The created file size is 109,592,584 bytes (and the "size on disk" is 109,596,672 bytes). Also checked if buffer contains null chars, did this: size_t pos = buffer.find(L'\0'); Expecting result to be std::wstring::npos but it is 18446744073709551615 , but my string

c++ can't convert string to wstring

蓝咒 提交于 2019-12-07 12:12:23
问题 I would like to convert a string variable to wstring due to some german characters that cause problem when doing a substr over the variable. The start position is falsified when any these special characters is present before it. (For instance: for "ä" size() returns 2 instead of 1) I know that the following conversion works: wstring ws = L"ä"; Since, I am trying to convert a variable, I would like to know if there is an alternative way for it such as wstring wstr = L"%s"+str //this is