Hi
I need to convert UTF8 string to ISO-8859-1 string using VB.NET.
Any example?
Thanks in advance.
Hi
I need to convert UTF8 string to ISO-8859-1 string using VB.NET.
Any example?
Thanks in advance.
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.
The encoding ISO-8859-1 is more commonly called Latin-1. You can get this encoding by doing the following
Dim latin1 = Text.Encoding.GetEncoding(&H6FAF)
The full conversion can be done by the following
Public Function ConvertUtf8ToLatin1(Dim bytes As Byte()) As Bytes()
Dim latin1 = Text.Encoding.GetEncoding(&H6FAF)
Return Encoding.Convert(Encoding.UTF8, latin1, bytes)
End Function
EDIT
As Jon pointed out, it may be easier for people to remember the decimal number 28591 rather than the hex number &H6FAF.
http://msdn.microsoft.com/en-us/library/system.text.encoding.convert.aspx
Try this with the variable "input" as the UTF-8 String;
VB.NET:
Dim result As Byte() = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), input);
C#:
byte[] result = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), input);
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
Hi
finally I've found 90% of solution:
smsMessage = Me.mmText.Text
smsMessage = System.Web.HttpUtility.UrlEncode(smsMessage , System.Text.Encoding.GetEncoding("ISO-8859-1"))
client.QueryString.Add("message", smsMessage )
Runs fine, except with the character +
It is ignored
Example:
I write: My name is Mary. 100+300
Returns: My name is Mary. 100300
But Encoded string is:
My+name+is+Mary.+100%2b300
How I can solve it?