-
Notifications
You must be signed in to change notification settings - Fork 1
/
string conv.h
62 lines (49 loc) · 1.23 KB
/
string conv.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <iostream>
#include <string>
// ref: https://docs.microsoft.com/en-us/cpp/text/how-to-convert-between-various-string-types?view=vs-2019
/*
template <typename T>
void printValue(const T& value)
{
cout << "Type: " << typeid(value).name() << "\n";
cout << "Value: " << dec << value << "\n";
cout << "Address: " << hex << value << "\n";
}
*/
wchar_t* charTowchar(const char* c)
{
size_t outSize;
const size_t cSize = strlen(c) + 1;
wchar_t* wStr = new wchar_t[cSize];
mbstowcs_s(&outSize, wStr, cSize, c, cSize - 1);
return wStr;
}
char* wcharTochar(const wchar_t* wc)
{
size_t outSize;
const size_t cSize = wcslen(wc) + 1;
char* cStr = new char[cSize];
wcstombs_s(&outSize, cStr, cSize, wc, cSize - 1);
return cStr;
}
wchar_t* wstringTowchar(const std::wstring &ws)
{
return const_cast<wchar_t*>(ws.c_str());
}
char* stringTochar(const std::string &s)
{
return const_cast<char*>(s.c_str());
}
std::wstring charTowstr(const char* c)
{
std::string cStr(c);
std::wstring wStr(cStr.begin(), cStr.end());
return wStr;
}
std::string wcharTocstr(const wchar_t* wc)
{
std::wstring wStr(wc);
std::string cStr(wStr.begin(), wStr.end());
return cStr;
}