stdstring

Convert C++ std::string to UTF-16-LE encoded string

强颜欢笑 提交于 2019-11-27 07:37:50
问题 I've been searching for hours today and just can't find anything that works out for me. The one I've just had a look at, with no luck, is " How to convert UTF-8 encoded std::string to UTF-16 std::string ". My question is, with a brief explanation: I want to make a valid NTLM hash in std C++, and I'm using OpenSSL's library to create the hash using its MD4 routines. I know how to do that, so does anyone know how to convert the std::string into a UTF-16 LE encoded string which I can pass to the

How do I construct a std::string from a DWORD?

99封情书 提交于 2019-11-27 07:35:38
问题 I have following code: Tools::Logger.Log(string(GetLastError()), Error); GetLastError() returns a DWORD a numeric value, but the constructor of std::string doesn't accept a DWORD . What can I do? 回答1: You want to read up on ostringstream: #include <sstream> #include <string> int main() { std::ostringstream stream; int i = 5; stream << i; std::string str = stream.str(); } 回答2: You want to convert the number to a string : std::ostringstream os; os << GetLastError(); Log(os.str(), Error); Or

Error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’

非 Y 不嫁゛ 提交于 2019-11-27 06:20:44
At the top of my file I have #define AGE "42" Later in the file I use ID multiple times including some lines that look like 1 std::string name = "Obama"; 2 std::string str = "Hello " + name + " you are " + AGE + " years old!"; 3 str += "Do you feel " + AGE + " years old?"; I get the error: "error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’" on line 3. I did some research and found it was because of how C++ was treating the different strings and was able to fix it by changing "AGE" to "string(AGE)." However, I accidentally missed one of the instances

Is it possible to use std::string in a constexpr?

六眼飞鱼酱① 提交于 2019-11-27 06:19:50
Using C++11, Ubuntu 14.04, GCC default toolchain . This code fails: constexpr std::string constString = "constString"; error: the type ‘const string {aka const std::basic_string}’ of constexpr variable ‘constString’ is not literal... because... ‘std::basic_string’ has a non-trivial destructor Is it possible to use std::string in a constexpr ? (apparently not...) If so, how? Is there an alternative way to use a character string in a constexpr ? tenfour No, and your compiler already gave you a comprehensive explanation. But you could do this: constexpr char constString[] = "constString"; At

How do I convert wchar_t* to std::string?

為{幸葍}努か 提交于 2019-11-27 05:51:08
问题 I changed my class to use std::string (based on the answer I got here but a function I have returns wchar_t *. How do I convert it to std::string? I tried this: std::string test = args.OptionArg(); but it says error C2440: 'initializing' : cannot convert from 'wchar_t *' to 'std::basic_string<_Elem,_Traits,_Ax>' 回答1: You could just use wstring and keep everything in Unicode 回答2: std::wstring ws( args.OptionArg() ); std::string test( ws.begin(), ws.end() ); 回答3: You can convert a wide char

How do I allocate a std::string on the stack using glibc's string implementation?

感情迁移 提交于 2019-11-27 05:34:20
问题 int main(void) { std::string foo("foo"); } My understanding is that the above code uses the default allocator to call new. So even though the std::string foo is allocated on the stack the internal buffer inside of foo is allocated on the heap. How can I create a string that is allocated entirely on the stack? 回答1: I wanted to do just this myself recently and found the following code illuminating: Chronium's stack_container.h It defines a new std::allocator which can provide stack-based

Explicit copy constructor

痞子三分冷 提交于 2019-11-27 05:16:35
I have extended std::string to fulfil my needs of having to write custom function build into string class called CustomString I have defined constructors: class CustomString : public std::string { public: explicit CustomString(void); explicit CustomString(const std::string& str); explicit CustomString(const CustomString& customString); //assignment operator CustomString& operator=(const CustomString& customString); ... }; In the third constructor (copy constructor) and assignment operator, whose definition is: CustomString::CustomString(const CustomString& customString): std::string(static

I want to convert std::string into a const wchar_t *

筅森魡賤 提交于 2019-11-27 03:48:39
Is there any method? My computer is AMD64. ::std::string str; BOOL loadU(const wchar_t* lpszPathName, int flag = 0); When I used: loadU(&str); the VS2005 compiler says: Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 ' to 'const wchar_t *' How can I do it? Matt Dillard If you have a std::wstring object, you can call c_str() on it to get a wchar_t* : std::wstring name( L"Steve Nash" ); const wchar_t* szName = name.c_str(); Since you are operating on a narrow string, however, you would first need to widen it. There are various options here; one is to use Windows' built

Can one leverage std::basic_string to implement a string having a length limitation?

北战南征 提交于 2019-11-27 03:32:32
问题 I'm working with a low-level API that accepts a char* and numeric value to represent a string and its length, respectively. My code uses std::basic_string and calls into these methods with the appropriate translation. Unfortunately, many of these methods accept string lengths of varying size (i.e. max( unsigned char ), max( short ), etc...) and I'm stuck writing code to make sure that my string instances do not exceed the maximum length prescribed by the low-level API. By default, the maximum

Encode/Decode std::string to UTF-16

纵饮孤独 提交于 2019-11-27 02:58:27
问题 I have to handle a file format (both read from and write to it) in which strings are encoded in UTF-16 (2 bytes per character). Since characters out of the ASCII table are rarely used in the application domain, all of the strings in my C++ model classes are stored in instances of std::string (UTF-8 encoded). I'm looking for a library (searched in STL and Boost with no luck) or a set of C/C++ functions to handle this std::string <-> UTF-16 conversion when loading from or saving to file format