Skip to content

Commit

Permalink
rewrite of wstring to string conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
noah1510 committed Dec 14, 2023
1 parent eb62c70 commit 6827f8f
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/rs232_native_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
#include <codecvt>
#include <locale>

static inline std::string wstrToStr(const std::wstring& wstr) noexcept {
if (wstr.empty()) {
static inline std::string wstrToStr(const std::wstring& wideStr) noexcept {
if (wideStr.empty()) {
return "";
}
auto size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo{size_needed, 0};
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
int bufferSize = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (bufferSize <= 0 ){return "";}

// Create a buffer to store the converted string
std::string narrowStr(bufferSize, '\0');

// Perform the conversion
auto conversion = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, &narrowStr[0], bufferSize, nullptr, nullptr);
return (conversion > 0) ? narrowStr : "";
}

inline HANDLE& getCport(void* portHandle) noexcept {
Expand Down

0 comments on commit 6827f8f

Please sign in to comment.