How to convert ANSI bytes to Unicode string?

I have a vector<BYTE> that represents the characters in a string. I want to interpret these characters as ASCII characters and store them in a Unicode string (UTF-16). The current code assumes that the characters in vector<BYTE> are Unicode, not ASCII. This works great for standard ASCII, but does not work for extended ASCII characters. These characters must be interpreted using the current code page obtained through GetACP() . How can I create a Unicode string (UTF-16) with these ASCII characters?

EDIT: I believe the solution should have something to do with the macros discussed here: http://msdn.microsoft.com/en-us/library/87zae4a3(v=vs.80).aspx I'm just not quite sure how the actual implementation will go.

 int ExtractByteArray(CATLString* pszResult, const CByteVector* pabData) { // place the data into the output cstring pszResult->Empty(); for(int iIndex = 0; iIndex < pabData->GetSize(); iIndex++) *pszResult += (TCHAR)pabData->GetAt(iIndex); return RC_SUCCESS; } 
+4
source share
3 answers

You must use MultibyteToWideChar to convert this string to unicode

+4
source

I have a vector<BYTE> that represents the characters in a string. I want to interpret these characters as ASCII characters and store them in a Unicode string (UTF-16)

You should use std::vector<BYTE> only when working with binary data. When working with strings, use std::string instead. Note that this std::string object will contain special characters that will be encoded in sequences of one or more bytes (the so-called multi - byte characters ), but this is not ASCII .

Once you use std::string , you can use MultiByteToWideChar to create your own function that converts std::string (which contains a few bytes of UTF-8 characters) to std::wstring containing UTF-16 encoded points:

 // multi byte to wide char: std::wstring s2ws(const std::string& str) { int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); std::wstring wstrTo(size_needed, 0); MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); return wstrTo; } 
+1
source

Since you are using MFC, let CString do the job. There. It was not at all difficult, was it?

+1
source

All Articles