aslze / asl

A compact C++ cross-platform library including JSON, XML, HTTP, Sockets, WebSockets, threads, processes, logs, file system, CSV, INI files, vectors and matrices, etc.
Other
69 stars 17 forks source link

Converting String to std::string #10

Closed kabukunz closed 5 years ago

kabukunz commented 5 years ago

Hello, first of all thanks for this very useful library... I hava a cmd line input param which I need to feed to another library. This library takes std::string type for the parameter. How can I convert asl::String to std::string? I've checked the String.h code but cannot find a method. Thank you

aslze commented 5 years ago

There is no direct conversion but you can get a const char* pointer with the * operator and create a std::string with it.

For example, to load an image with OpenCV, which uses std::string as input:

auto file = cmdargs["file"];
cv::imread(*file);

For the other way, you can construct asl::String with a const char*, so use the c_str() function:

std::string stdstring = *aslstring;
asl::String aslstring = stdstring.c_str();

In functions receiving const char* or const wchar_t* you can just pass asl::String and the conversion will be automatic (the latter case will convert to UTF16).

kabukunz commented 5 years ago

Ah, almost got it! But I missed the correct syntax I guess... Thank you for your prompt answer. I've tried and now it works fine. Thx again