Encoding UTF8 to / from Unicode

O

oLiVieR CheNeSoN

Hello,


I would like to be sure that i can convert Unicode to uTF8 and vice versa.

Can you check this code ? in VB.net and tell me if it is ok
Thanks
Olivier



Private Sub ButtonUniToUTF8_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonUniToUTF8.Click

Dim unicodeString As String = Me.RichTextBoxUnicode.Text

' Create two different encodings.

Dim dstEncoding As Encoding = Encoding.UTF8

Dim dstEncodingANSI As Encoding = Encoding.GetEncoding(1252)

Dim srcEncoding As Encoding = Encoding.Unicode

' Convert the string into a byte[].

Dim srcBytes As Byte() = srcEncoding.GetBytes(unicodeString)

' Perform the conversion from one encoding to the other.

Dim dstBytes As Byte() = Encoding.Convert(srcEncoding, dstEncoding,
srcBytes)

Me.RichTextBoxUTF8.Text = dstEncodingANSI.GetString(dstBytes)



End Sub

Private Sub ButtonUTF8ToUni_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonUTF8ToUni.Click

Dim UTF8String As String = Me.RichTextBoxUTF8.Text

' Create two different encodings.

Dim dstEncoding As Encoding = Encoding.Unicode

Dim dstEncodingANSI As Encoding = Encoding.GetEncoding(1252)

Dim srcEncoding As Encoding = Encoding.UTF8

' Convert the string into a byte[].

Dim srcBytes As Byte() = dstEncodingANSI.GetBytes(UTF8String)

' Perform the conversion from one encoding to the other.

Dim dstBytes As Byte() = Encoding.Convert(srcEncoding, dstEncoding,
srcBytes)

Me.RichTextBoxUnicode.Text = dstEncoding.GetString(dstBytes)

End Sub
 
M

Mattias Sjögren

I would like to be sure that i can convert Unicode to uTF8 and vice versa.
Can you check this code ? in VB.net and tell me if it is ok

Well you're converting Unicode -> UTF8 -> ANSI -> Unicode. I'm not
sure what you're trying to accomplish, but I don't think this is
really what you want to do. And interpreting UTF8 data as ANSI will
not give you the correct result.

If you really just want to convert to/from UTF8, you should stop after
the Encoding.UTF8.GetBytes call and keep the data as a byte array.



Mattias
 
O

oLiVieR CheNeSoN

Hi Mattias

Thanks for your reply.

I have tried without using CP1252 and Unicode but it doesnt seems to work.

That what i want to have

Unicode character = ?

UTF8 = æ°'


Thanks
Olivier
 

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

Similar Threads

Convert OEM to Unicode 4
Encoding question 1
Debugging Error 1
XML Encoding woes... 4
Using RichTextBox Read/Write Unicode File 0
httpwebrequest 2
String translation 2
Converting textfile from Mac to Windows 2

Top