Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encoding-Decoding

Status
Not open for further replies.

prasadmokashi

Programmer
Oct 31, 2003
41
US
Hi,

This is my first encounter with C# and having problem with the encoding.

One process is sending me the byte array which may contain DBCS and Unicode characters ( such as russian, chinese ).

I want to convert it to string ( using unicode encoding I guess.......), such that unicode characters remain intact.

Could anybody please help me out.

Thanks,
Prasad
 
You will need to know if the byte array contains Unicode (UTF-32, UTF-16, UTF-8, UTF-7, etc.) or DBCS characters (ie. codepage values).

If UTF-16 Unicode, do this:
Code:
UnicodeEncoding unicode = new UnicodeEncoding();
string MyString = unicode.GetString(MyBytes);
You can do a similar thing with the UTF8Encoding class for UTF-8 style Unicode.

If you have a DBCS byte array, you need to create an encoding object based on the codepage of the characters:
Code:
Encoding encode = new Encoding.GetEncoding(932);
string MyString = encode.GetString(MyBytes);
The value 932 refers to the Shift-JIS codepage (you can also use 1252 for the windows-1252 codepage). You will need to find the value in the Windows internationalization documentation for the codepage of the data that is being sent to you. Unfortunately, there aren't any constants defined for these values.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top