Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string to korean encoding

Tags:

c#

encoding

I am extracting the data of a korean game (Ragnarok Online) to build up a database for it. I had no problems for years to convert the strings from ISO-8859-1 encoding to EUC-KR encoding in c#.

The function I used to convert the string was this.

return Encoding.GetEncoding("EUC-KR").GetString(Encoding.GetEncoding("ISO-8859-1").GetBytes(text);

Example

º»Ç︧ -> 본헬름

Now I encounter some characters not converting correctly and I am not sure why.

The ansi string is converted to

Œc¾ç²á -> Oc양꿍

which is wrong I assume. I tested a bit with encoding in notepad++ and if I convert the string to Korean (Windows-949 instead of EUC-KR) it shows the correct one.

똠양꿍

But in C#, if I use Codepage 949 it still converts to the wrong one. The codepage "Windows-949" is not known in .net framework.

What is the correct encoding for this or is the source string just wrong?

Thank you very much.

/edit: problem solved. seems like ISO-8859-1 AND EUC-KR were wrong. If I convert from 1252 -> 949 it's fine.

like image 230
mrdiablo Avatar asked Dec 12 '25 14:12

mrdiablo


1 Answers

Thanks for adding the edit with the answer to your question. I had data from an old database where the data was stored in a Korean language encoding that was not displaying correctly.

After hours of playing with the code to convert the data to a different encoding, I came across your question. I did the Code Page 1252 to Code Page 949 conversion, and then I started getting valid Korean words out of the database entries.

My code context is from a .NET forms web application, and I'm including the Page_load function below to provide sample code on how to convert from one encoding to another:

protected void Page_Load(object sender, EventArgs e)
{

    string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["DictionaryConnection"].ToString();
    using (SqlConnection conn = new SqlConnection(strConn))
    {
        conn.Open();

        string strSQL = "Select top(100) * from Parts";


        SqlCommand command = new SqlCommand(strSQL, conn);

        SqlDataReader sdr = command.ExecuteReader();

        DataTable dt = new DataTable();

        dt.Load(sdr);


        sdr.Close();


        StringBuilder sbOut = new StringBuilder();


        sbOut.Append("<table border=\"1\">");
        sbOut.Append("<tr>");
        foreach (DataColumn dc in dt.Columns)
        {
            sbOut.Append("<th>" + dc.ColumnName + "</th>");

        }

        sbOut.Append("</tr>");

        foreach (DataRow dr in dt.Rows)
        {

        sbOut.Append("<tr>");
        foreach (DataColumn dc in dt.Columns)
        {
            string strOut = "";
            if (dr[dc] != null)
            {
                if (dc.ColumnName=="Part_h")
                {


                    int euckrCodepage = 949;//949;//51949;

                    System.Text.Encoding originalEncoding = System.Text.Encoding.GetEncoding(1252);


                    System.Text.Encoding euckr = System.Text.Encoding.GetEncoding(euckrCodepage);
                    StringBuilder sbEncoding= new StringBuilder();


                    sbEncoding.Append("RAW: " + dr[dc].ToString() + "<br />");


                   byte[] rawbytes= originalEncoding.GetBytes(dr[dc].ToString());


                   string s = euckr.GetString(rawbytes);
                    sbEncoding.Append("STRING AS "+euckr.EncodingName+": " + s + "<br />");


                    strOut = sbEncoding.ToString();
                }
                else
                {
                strOut = dr[dc].ToString();    
                }

            }

            sbOut.Append("<td>" + strOut + "</td>");

        }

            sbOut.Append("</tr>");
        }

        sbOut.Append("</table>");

    conn.Close();
    lblText.Text = sbOut.ToString();
    }






}
like image 137
WWC Avatar answered Dec 15 '25 02:12

WWC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!