ascii string Forlì
and using the below loop
string s = "Forlì";
for(size_t i = 0; i < s.size(); i++) {
printf("% d", s[i]);
}
wstring s = L"Forlì";
for(size_t i = 0; i < s.size(); i++) {
printf("% d", s[i]);
}
this is what I get
70 111 114 108 -61 -84 //string
70 111 114 108 236 //wstring
Now I want to convert between these types without changing whatever values they are printing above(not some different encoding). Since I'm not using windows or c++11, i found the below routines which gives me unsatisfactory results
std::wstring s2ws(const std::string& s) {
const char* _Source = s.c_str();
size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
return result;
}
std::wstring s2ws(const std::string& s) {
wstring w;
w.assign(s.begin(), s.end());
return w;
}
When I print the converted string using 1st routine it does not output anything while the second routine does not convert it to wstring(the -61,-84 do not get changed to 236).
How can I do this conversion properly? even an algorithm to manipulate those numbers without any library function will suffice...