Does the Encoding class support ISO-2022-xx to Unicode conversion?

  • Thread starter Thread starter Richard Lewis Haggard
  • Start date Start date
R

Richard Lewis Haggard

I'm looking for some means to convert ISO-2022-xx encoded textual data to
Unicode. Is there some mechanism in .NET to do this? The Encoding class looks
promisiing.
 
Confirmed. Encoding class will easily convert from ISO-2022-xx to Unicode.
Here's a code snippit demonstrating the basic operation: (note: second
attempt to post. First attempt ended up in some indeterminant failure wherein
IE was hung)

namespace TestIsoToUnicode
{
class Program
{
static void Main(string[] args)
{
// Starting string, a name of a file in Japanese as expressed in
ISO-2022-JP
char[] stringInJpnIsoEncoding = new char[] {
(char)0x1b, (char)0x24, (char)0x42, (char)0x25, (char)0x22,
(char)0x25,
(char)0x2f, (char)0x25, (char)0x3b, (char)0x25, (char)0x39,
(char)0x38,
(char)0x22, (char)0x43, (char)0x2a, (char)0x32, (char)0x37,
(char)0x24,
(char)0x37, (char)0x34, (char)0x30, (char)0x4e, (char)0x3b,
(char)0x4a,
(char)0x73, (char)0x1b, (char)0x28, (char)0x42, (char)0x1b,
(char)0x24,
(char)0x42, (char)0x39, (char)0x70, (char)0x3d, (char)0x71,
(char)0x1b,
(char)0x28, (char)0x42, (char)0x2e, (char)0x64, (char)0x6f,
(char)0x63,
(char)0x00 };

// Conversion requires in input format and output format
specifier.
// This is the source format object.
Encoding jpnEncoding = Encoding.GetEncoding(50220);

byte[] convertedToUnicode = Encoding.Convert(
jpnEncoding, // Src format
Encoding.Unicode, // Dst format
jpnEncoding.GetBytes(stringInJpnIsoEncoding)); //
Dst for data

// Turn Unicode formated data's byte array into a string object.
string strUni = Encoding.Unicode.GetString( convertedToUnicode );
}
}
}
 
Richard Lewis Haggard said:
I'm looking for some means to convert ISO-2022-xx encoded textual data to
Unicode. Is there some mechanism in .NET to do this? The Encoding class
looks
promisiing.

Yes, take a look at
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx.
System.Text.Encoding supports ISO-2022-jp and ISO-2022-kr as encodings
50220, 50222 and 50225.

using System.Text;
Encoding enc = Encoding.GetEncoding(50220);
using (StreamReader sr = new StreamReader(file, enc))
{
string unicode = sr.ReadToEnd();
//...
}
 
Back
Top