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.


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;

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]);

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.