I'm getting 16 bits from a struct in memory, and I need to convert them into a string. The 16 bits represent a unicode char:
typedef struct my_struct {
unsigned unicode : 16;
} my_struct;
I started by casting the bits into an unsigned char, which worked for values small enough to fit in one char. However, for characters like '♪', it truncates incorrectly. This is what I have so far:
char buffer[2] = { 0 };
wchar_t wc[1] = { 0 };
wc[0] = page->text[index].unicode;
std::cout << wc[0] << std::endl; //PRINT LINE 1
int ret = wcstombs(buffer, wc, sizeof(buffer));
if(ret < 0)
printf("SOMETHING WENT WRONG \n");
std::string my_string(buffer);
printf("%s \n", my_string.c_str()); //PRINT LINE 2
Print line 1 currently prints: "9834" and print line 2 prints: "" (empty string). I'm trying to get my_string to contain '♪'.
std::wstringholds units ofwchar_t, which may be UTF-16). If neither of those works, you could instantiatestd::basic_stringover yourmy_structdirectly:std::basic_string<my_struct> whatever;