Unicode to UTF8-String

T

Tim Daim

Sorry, this is my second question:

How can I convert a Unicode string to a UTF-8-String?

Thanks!
Tim
 
M

Martin Honnen

Tim said:
How can I convert a Unicode string to a UTF-8-String?

Strings in the .NET framework are sequences of UTF-16 encoded characters
so there is nothing like an UTF-8-String in the .NET framework, unless
you wanted to implement your own string class that internally uses UTF-8.
 
M

Martin Honnen

Tim said:
But if I want to post a Unicode string to a website in UTF-8 format...?

Well on a stream you deal with bytes and you can certainly use UTF-8 as
the encoding of a stream.
What exactly do you want to achieve? Do you want to make a HTTP POST
request? What content type should the request body have (e.g.
application/x-www-form-urlencoded or text/plain or something else)?
 
T

Tim Daim

strPostData &= "&text=" & m_strText

The m_strText should be converted to UTF-8, but I'm still fighting with
the StreamWriter... I don't know how it works "on the fly"... :-(

This is the function I want to use:

''' <summary>
''' Retrieves the POST data (if any) to be sent to the url to be fetched.
''' The data is returned as a string of the form "arg=val[&arg=val]...".
''' </summary>
''' <returns>A string containing the POST data or null if none.</returns>
Protected Overrides Function getPostData() As String
' Set translation mode
Dim strPostData As String =
"hl=en&ie=UTF8&oe=UTF8submit=Translate&langpair="

Select Case m_mode
Case Mode.EnglishToFrench
strPostData &= "en|fr"
Case Mode.FrenchToEnglish
strPostData &= "fr|en"
Case Mode.EnglishToGerman
strPostData &= "en|de"
Case Mode.GermanToEnglish
strPostData &= "de|en"
Case Mode.EnglishToSpanish
strPostData &= "en|es"
Case Mode.SpanishToEnglish
strPostData &= "es|en"
Case Mode.EnglishToItalian
strPostData &= "en|it"
Case Mode.ItalianToEnglish
strPostData &= "it|en"
Case Mode.EnglishToPortugese
strPostData &= "en|pt"
Case Mode.PortugeseToEnglish
strPostData &= "pt|en"
Case Mode.EnglishToDanish
strPostData &= "en|da"
Case Mode.DanishToEnglish
strPostData &= "da|en"
Case Mode.GermanToDanish
strPostData &= "de|da"
Case Mode.danishtogerman
strPostData &= "da|de"
End Select

'//My attempt
Dim encode As Text.Encoding =
System.Text.Encoding.GetEncoding("utf-8")
Dim w As System.IO.StreamWriter
w.
'\\\end of my attempt

' Set text to be translated
strPostData &= "&text=" & m_strText
Return strPostData
End Function
 
T

Tim Daim

I want to use the google translation API. I send the query string to the
website, then fetch the return. The return seems to work correctly, but
I haven't managed to encode for example Umlauts to something that the
website accepts.
 
G

Göran Andersson

Tim said:
Sorry, this is my second question:

How can I convert a Unicode string to a UTF-8-String?

Thanks!
Tim

I believe that this is the answer to what you throught that you were
asking: ;)

Dim data As Byte() = Encoding.UTF8.GetBytes(theString)

It encodes a string (which always is Unicode) into UTF-8. The result is
not a string, but an array of bytes.
 
M

Michel Posseth [MCP]

try UrlEncode(...)


Imports System.Web to the top with the rest of your Imports
or use it like
System.Web.HttpUtility.UrlEncode(...)

HTH

Michel Posseth [MCP]
 

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