i need help on how to convert ucs2 to utf8 i dont want to use any functions that instantly converts the 2 what i want to do is that from what i have read about this topic that ucs2 has 16 bits i want to convert the 16 bits to 8bits can you help me out on how should i start

Recommended Answers

All 3 Replies

Unless you're doing this to learn about the encodings, I'd recommend using a library that does it for you. I usually recommend ICU.

Unless you're doing this to learn about the encodings, I'd recommend using a library that does it for you. I usually recommend ICU.

Also, since it is an open source library, you should be able to get the source code for the code page conversion routines and inspect/analyze them in order to understand what the algorithms are that they use. There may also be documentation on those, but I didn't look into the site far enough to determine that. Good reference Narue! Thanks! :-)

//UCS-2 INPUT            
string hex = textBox1.Text.Replace(" ", "");

//UCS-2 BYTES TO DECIMAL VALUES
int b3 = Convert.ToInt32("00" + hex[4] + hex[5], 16) - 128;
int b2 = (Convert.ToInt32("00" + hex[2] + hex[3], 16)- 128) * 64;
int b1 = (Convert.ToInt32("00" + hex[0] + hex[1], 16)- 224) * 4096;

//COUNT ALL DECIMAL VALUES
int unicode = b1 + b2 + b3; 

//CONVERT DECIMAL VALUE TO HEXADECIMAL (UNICODE)
textBox2.Text = unicode.ToString("X4");

This works for all UCS-2 character codes starting from 2048
Below 2048 you must use another algorythm

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.