string encoding problem

G

Guest

Hi Al

I'd like to encode a string submitted from a utf-8 form in a aspx page to big5
Any ideas on how to do that
I try sth like

public static string unicode_big5(string src) {
Encoding big5 = Encoding.GetEncoding("big5")
Encoding unicode = Encoding.UTF8
byte[] unicodeBytes = unicode.GetBytes(src)
return big5.GetString(unicodeBytes)

But it doesn't wor

Thanks in advanc
 
J

Jon Skeet [C# MVP]

WEIWEIWEI said:
I'd like to encode a string submitted from a utf-8 form in
a aspx page to big5.
Any ideas on how to do that?
I try sth like:

public static string unicode_big5(string src) {
Encoding big5 = Encoding.GetEncoding("big5");
Encoding unicode = Encoding.UTF8;
byte[] unicodeBytes = unicode.GetBytes(src);
return big5.GetString(unicodeBytes);
}

A string is just a sequence of characters - it doesn't have an
encoding, really. (Internally it's in UTF-16, but that's not really
important.)

What you're doing above is finding the UTF-8 encoded byte sequence for
a string, and then trying to decode that as if it were a Big5 encoded
byte sequence.

The browser should be setting its content type on the response, and
submitting the data in the encoding it specifies. ASP.NET will decode
that appropriately, and then you've got a string - you shouldn't need
to worry beyond that.

Now, what problem are you actually trying to solve?

Look at these articles for more information and ideas:
http://www.pobox.com/~skeet/csharp/unicode.html
http://www.pobox.com/~skeet/csharp/debuggingunicode.html
 
G

Guest

Hi

What I am doing is
I got a form in a utf-8 page
I get data from that form, and want to submit the data in email using a 3rd party compoent in Big5 format
so that's what I am now working on

originally using the .NET MailMessage Class, I can do sth like
message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312")

but now I can't do that so that's y I;d like to change the encoding of the string I go

Thanks for you help
 
J

Jon Skeet [C# MVP]

WEIWEIWEI said:
What I am doing is:
I got a form in a utf-8 page.
I get data from that form, and want to submit the data in email using
a 3rd party compoent in Big5 format.
so that's what I am now working on.

Right. The fact that the form is in UTF-8 is irrelevant then.
originally using the .NET MailMessage Class, I can do sth like:
message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");

but now I can't do that so that's y I;d like to change the encoding
of the string I got

You can't change the encoding of a string - a string itself hasn't
*got* an encoding.

Now, why can't you set the BodyEncoding any more?

What format are you submitting the data to the 3rd party component in?
If it's as a byte array, you can get the Big5-encoded byte array of
your string. If it's as a string, you'll have to find some other way of
telling it to use Big5.
 

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

Top