0

In C++, I would like to save hexadecimal string into file as unicode character Ex: 0x4E3B save to file ---> 主

Any suggestions or ideas are appreciated.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
user586219
  • 1
  • 1
  • 1
  • 1
    Use the C++ language tag otherwise lots of people who would be able to answer your question will never even see it. – Mark Byers Jan 23 '11 at 08:40
  • 2
    What does the string 0x4E3B represent? A code point? Two bytes is too short for a code point. And when you want to save it to a file, what encoding is that file using? – David Heffernan Jan 23 '11 at 08:48
  • @David - it's a code point (U+4E3B). It's [0xE4B8BB](http://translate.google.com/#ja|en|%E4%B8%BB) in UTF-8. – Seth Jan 23 '11 at 09:12
  • @Seth How does that help the OP? Why is UTF-8 relevant? – David Heffernan Jan 23 '11 at 09:14
  • David: it could be UTF-16, or he could mean U+4E3B. Both would be 主. – Konrad Rudolph Jan 23 '11 at 09:14
  • @Konrad Yes I understand that, my comment is meant to provoke some thought by the OP? I wasn't asking because I actually wanted to know myself!! – David Heffernan Jan 23 '11 at 09:16
  • @David - if he was outputting U+4E3B to a file, chances are probably good that he'd be doing it in UTF-8. Knowing the what the code point looks like in UTF-8 would help confirm that he did it correctly. – Seth Jan 24 '11 at 20:04

1 Answers1

1

What encoding? I assume UTF-8.

What platform?

If you under Linux then

std::locale loc("en_US.UTF-8"); // or "" for system default
std::wofstream file;
file.imbue(loc); // make the UTF-8 locale for the stream as default
file.open("file.txt");   
wchar_t cp = 0x4E3B;
file << cp;

However if you need Windows it is quite different story:

You need to convert code point to UTF-8. Many ways. If it is bigger then 0xFFFF then convert it to UTF-16 and then search how to use WideCharToMultiByte, and then save to file.

Luka
  • 83
  • 1
  • 9
Artyom
  • 31,019
  • 21
  • 127
  • 215