Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

utf 8 - Encoding UTF8 string to ISO-8859-1 String (VB.NET)

I need to convert UTF8 string to ISO-8859-1 string using VB.NET.

Any example?


emphasized textI have tried Latin function and not runs. I receive incorrect string.

My case is that I need to send SMS using API.

Now I have this code:

        baseurl = "http://www.myweb.com/api/sendsms.php"
        client = New WebClient
        client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
        client.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1")
        client.QueryString.Add("user", user)
        client.QueryString.Add("password", pass)
        client.QueryString.Add("alias", myAlias)
        client.QueryString.Add("dest",  mobile)
        textoSms = Me.mmTexto.Text
        textoSms = System.Web.HttpUtility.UrlEncode(textoSms)
        client.QueryString.Add("message", textoSms)
        data = client.OpenRead(baseurl)
        reader = New StreamReader(data)
        s = reader.ReadToEnd()
        data.Close()
        reader.Close()

But not runs...I receive incorrect messages. For example

if I write: ma?ana returns maa ana

If I write aigüa returns aiga

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about:

Dim converted as Byte() = Encoding.Convert(utf8, Encoding.UTF8, _
                                           Encoding.GetEncoding(28591))

That assumes that when you say "UTF8 string" you mean "binary data which is the UTF-8 representation of some text". If you mean something else, please specify :)

Note that ISO-8859-1 only represents a tiny proportion of full Unicode. IIRC, you'll end up with "?" for any character from the source data which isn't available in ISO-8859-1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...