UTF8 String Encoding in ISO-8859-1 String (VB.NET)

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

Any example?


underlined text I tried the latin function and did not start. I get the wrong string.

My thing is that I need to send SMS using the 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 it doesn’t work ... I get the wrong messages. for example

if I write: mañana returns maa ana

If I write aigüa, return aiga

+3
source share
5 answers

What about:

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

, " UTF8", " , UTF-8 ". - , :)

, ISO-8859-1 Unicode. IIRC, "?" , ISO-8859-1.

+8

ISO-8859-1 -1. ,

Dim latin1 = Text.Encoding.GetEncoding(&H6FAF)

Public Function ConvertUtf8ToLatin1(Dim bytes As Byte()) As Bytes()
  Dim latin1 = Text.Encoding.GetEncoding(&H6FAF)
  Return Encoding.Convert(Encoding.UTF8, latin1, bytes)
End Function

, 28591, H6FAF.

+3

System.Text.Encoding.GetEncoding("ISO-8859-1") ñ, , SMS.

, Unicode ( !)

+1

http://msdn.microsoft.com/en-us/library/system.text.encoding.convert.aspx

"input" UTF-8;

VB.NET:

Dim result As Byte() = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), input);

#:

byte[] result = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), input);
0

, , #, , .

, ...

/// <summary>
/// Function for checking if a string can support the target encoding type
/// </summary>
/// <param name="text">The text to check</param>
/// <param name="targetEncoding">The target encoding</param>
/// <returns>True if the encoding supports the string and false if it does not</returns>
public bool SupportsEncoding(string text, Encoding targetEncoding)
{
    var btext = Encoding.Unicode.GetBytes(text);
    var bencodedtext = Encoding.Convert(Encoding.Unicode, targetEncoding, btext);

    var checktext = targetEncoding.GetString(bencodedtext);
    return checktext == text;
}

//Call the function demo with ISO-8859-1/Latin-1
if (SupportsEncoding("some text...", Encoding.GetEncoding("ISO-8859-1")))
{
    //The encoding is supported
}
else
{
    //The encoding is not supported 
}
0

Source: https://habr.com/ru/post/1718924/


All Articles