Encoding UTF8 to / from Unicode

  • Thread starter Thread starter oLiVieR CheNeSoN
  • Start date Start date
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
 
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
 
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
 
Back
Top