const void * to std::wstring

Hi,

So I've been reading up on void pointers and understand that it lets the compiler know it points to a variable of unknown type, but how to manipulate them/ use them is still really confusing. For instance,

1
2
3
wstring str_hello = L"hello";

void* pv = static_cast<void*>(const_cast<wchar_t*>(str_hello.c_str()));


casts the string to a void pointer, but I can't figure out how to do it the other way around. For instance, sqlite3_errmsg16() returns a const void *, and I need to be able to transform that into a wstring or string.

How may I accomplish this?

Thank you for any help.
a quick search of sqlite3_errmsg16() shows it returns a const pointer to a UTF-16 encoded array.

The tricky bit is that wchar_t isn't guaranteed to be 16-bits (and in fact it isn't on many platforms -- on some it is larger).

If you are only concerned about windows, you can do this:

 
wstring s = static_cast<const wchar_t*>(sqlite3_errmsg16());


Although as mentioned this will only work if wchar_t is 16 bits (which AFAIK is pretty much only true on Windows)

If you want to be "safe" you'll need to get clever about UTF-16 decoding and convert to an appropriate string type.
I just came up with the same solution!

What other type of string can represent wide characters instead of wchar_t/wstring? If I want to go the safe route like you said, and learn about UTF-16 decoding, so that I can move on to cross platform programs, would I need to download a specific unicode library? Is TCHAR cross platform?

Thank you again!
I'm probably thinking of using the ICU library to accomplish cross-platform portability.
I've been meaning to write a small lightweight UTF conversion lib for weeks now.

Something like this is really trivial if you're familiar with UTF encodings.
Last edited on
Topic archived. No new replies allowed.