Converting ASCII to UTF-8

A

Alci

I am getting some Korean characters data from MS SQL server. These
data were submitted as UTF-8 into the database, but stored as normal
varchars. So, when I getting them out of database by using Gridview
+SqlDataSource, they are actually ASCII format, but I couldn't just
convert the encoding of the page to get the proper UTF-8 format Korean
texts.

So, i modified a function from MS site, try to convert ASCII to UTF-8,
as below
protected string Convertor(string asciiString)
{
System.Text.Encoding ascii = System.Text.Encoding.ASCII;
System.Text.Encoding utf8 = System.Text.Encoding.UTF8;

byte[] asciiBytes = ascii.GetBytes(asciiString);
byte[] utf8Bytes = System.Text.Encoding.Convert(ascii, utf8,
asciiBytes);

char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0,
utf8Bytes.Length)];
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
string utf8String = new string(utf8Chars);

return utf8String;
}

But this doesn't work, all i get are question marks ???.

Anyone knows how do i solve this problem? From the SQL server side or
in the ASP.NET code?
 
A

Alci

But actually I still can get the Korean text back, if i save the text
in ASCII, then open it in the browser then select UTF-8 as encoding, i
just want to do it automatically by the script

They would need to be stored in the database as NVarchar in order for you not
to lose the UTF-8 information.

--Peter
"Inside every large program, there is a small program trying to get out."http://www.eggheadcafe.comhttp://petesbloggerama.blogspot.comhttp://www.blogmetafinder.com

Alci said:
I am getting some Korean characters data from MS SQL server. These
data were submitted as UTF-8 into the database, but stored as normal
varchars. So, when I getting them out of database by using Gridview
+SqlDataSource, they are actually ASCII format, but I couldn't just
convert the encoding of the page to get the proper UTF-8 format Korean
texts.
So, i modified a function from MS site, try to convert ASCII to UTF-8,
as below
protected string Convertor(string asciiString)
{
System.Text.Encoding ascii = System.Text.Encoding.ASCII;
System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
byte[] asciiBytes = ascii.GetBytes(asciiString);
byte[] utf8Bytes = System.Text.Encoding.Convert(ascii, utf8,
asciiBytes);
char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0,
utf8Bytes.Length)];
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
string utf8String = new string(utf8Chars);
return utf8String;
}
But this doesn't work, all i get are question marks ???.
Anyone knows how do i solve this problem? From the SQL server side or
in the ASP.NET code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top