0

Hello!

I'm wondering how to go about packing data from an std::vector<unsigned long> into valid unicode (UTF-8) characters. So far, I've tried writing the unsigned long data directly to a text file, but that usually results in invalid characters.

(I don't actually need to store the data in unsigned long form, I just thought it might make converting the data to unicode characters easier.)

Sorry about not posting any code, but I don't think what I have (above) is worth posting.

Thanks

Edited by wheel: n/a

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by wheel
0

UNICODE characters is wchar_t, which on MS-Windows is unsigned short and on some other operating systems can be unsigned long. So why not just do something like this?

size_t i;
wchar_t buf[20];
vector<unsigned long> characters;
for(i = 0; i < characters.size(); i++)
   buf[i] = characters[i];
buf[i] = 0;

Edited by Ancient Dragon: n/a

0

Thanks for the reply!

I've tried a similar method, and it didn't work (either resulted in errors when opening the file in a unicode text editor, or simply didn't print the characters). Here's my similar code: (with namespace std, of course)

wofstream outfile("output.txt", ios_base::trunc);
    vector<unsigned long> compressed = compress(inputstr, charmap);
    wchar_t* mychar;
    for(int i = 0; i < compressed.size(); ++i) {
        mychar = reinterpret_cast<wchar_t *>(&compressed[i]);
        outfile<<*mychar;
    }
    outfile<<endl;
    outfile.close();

Because unicode has certain ranges of values that are invalid, I don't think that this method will work in my case.

Edited by wheel: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.