943,667 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2276
  • C++ RSS
Sep 7th, 2009
0

Packing unsigned long into Unicode characters

Expand Post »
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
Last edited by wheel; Sep 7th, 2009 at 5:17 am.
Similar Threads
Reputation Points: 82
Solved Threads: 1
Newbie Poster
wheel is offline Offline
16 posts
since Aug 2009
Sep 7th, 2009
-7

Re: Packing unsigned long into Unicode characters

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?
C++ Syntax (Toggle Plain Text)
  1. size_t i;
  2. wchar_t buf[20];
  3. vector<unsigned long> characters;
  4. for(i = 0; i < characters.size(); i++)
  5. buf[i] = characters[i];
  6. buf[i] = 0;
Last edited by Ancient Dragon; Sep 7th, 2009 at 9:17 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Sep 7th, 2009
0

Re: Packing unsigned long into Unicode characters

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)
C++ Syntax (Toggle Plain Text)
  1. wofstream outfile("output.txt", ios_base::trunc);
  2. vector<unsigned long> compressed = compress(inputstr, charmap);
  3. wchar_t* mychar;
  4. for(int i = 0; i < compressed.size(); ++i) {
  5. mychar = reinterpret_cast<wchar_t *>(&compressed[i]);
  6. outfile<<*mychar;
  7. }
  8. outfile<<endl;
  9. outfile.close();
Because unicode has certain ranges of values that are invalid, I don't think that this method will work in my case.
Last edited by wheel; Sep 7th, 2009 at 3:48 pm.
Reputation Points: 82
Solved Threads: 1
Newbie Poster
wheel is offline Offline
16 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Converting a bit stream to binary
Next Thread in C++ Forum Timeline: Help with tick tack toe





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC