Chinese unicode characters to hex output

Jan 11, 2012 at 4:03pm
Hi,

Is it possible to take the input from a textbox that's in chinese (unicode) and convert it into hex to output to a text file?

Any insight would be great, thanks!
Jan 13, 2012 at 11:23am
Sure.
How is the text stored (accessible) in your code? Is it a wchar_t* or wstring? Either way, you'll want to find it's length in bytes and have a pointer to the string. Then convert that pointer to a char* and do file << hex << (int)char_ptr[i] for i from 0 to number of bytes.
Jan 18, 2012 at 3:41pm
Hi,

I was using this line of code to get the text from the textbox in languages that don't have chinese characters:

 
std::string str1 = (const char*)(Marshal::StringToHGlobalAnsi(Translation->Text)).ToPointer();


and then outputting it to the text file.

I assume to do this with chinese characters i'd start with this:

 
std::wstring str1 = (const wchar_t*)(Marshal::StringToHGlobalUni(Translation->Text)).ToPointer();


Is that correct?

Picking up on your point, how do you convert from wchar_t* to char*?

Thanks
Jan 18, 2012 at 4:22pm
You just force the conversion. char* ptr = (char*)my_wchar_t_pointer;. Although I don't know how much this is necessary. wchar_t is a numeric value like any other. You could do
1
2
for(int i = 0; i < str.length(); i++)
   file << std::hex << (int)str[i];

Although this could be not exactly what you want.
Topic archived. No new replies allowed.