将 UTF8 字符串编码为 ISO-8859-1 字符串 (VB.NET)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1499397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encoding UTF8 string to ISO-8859-1 String (VB.NET)
提问by aco
I need to convert UTF8 string to ISO-8859-1 string using VB.NET.
我需要使用 VB.NET 将 UTF8 字符串转换为 ISO-8859-1 字符串。
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.
我的情况是我需要使用 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?anareturns maa ana
如果我写:ma?ana返回maa ana
If I write aigüareturns aiga
如果我写aigüa返回aiga
回答by Jon Skeet
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 :)
假设当您说“UTF8 字符串”时,您的意思是“二进制数据,它是某些文本的 UTF-8 表示”。如果你的意思是别的,请说明:)
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.
请注意,ISO-8859-1 仅代表完整 Unicode 的一小部分。IIRC,你最终会得到“?” 对于 ISO-8859-1 中不可用的源数据中的任何字符。
回答by JaredPar
The encoding ISO-8859-1 is more commonly called Latin-1. You can get this encoding by doing the following
编码 ISO-8859-1 通常称为 Latin-1。您可以通过执行以下操作来获取此编码
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.
正如 Jon 指出的那样,人们可能更容易记住十进制数 28591 而不是十六进制数 &H6FAF。
回答by Daniel Tiru
Because System.Text.Encoding.GetEncoding("ISO-8859-1")
does not support ?
is my guess, in that case you need to use another encoding type for you SMS.
因为System.Text.Encoding.GetEncoding("ISO-8859-1")
不支持?
是我的猜测,在这种情况下,您需要为您的 SMS 使用另一种编码类型。
回答by Daniel Tiru
Dont know if this should be posted here but i made a small function in C# to check if a string support the target encoding type.
不知道这是否应该张贴在这里,但我在 C# 中做了一个小函数来检查字符串是否支持目标编码类型。
Hope it can be of any help...
希望它可以有任何帮助...
/// <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
}
回答by Omar
http://msdn.microsoft.com/en-us/library/system.text.encoding.convert.aspx
http://msdn.microsoft.com/en-us/library/system.text.encoding.convert.aspx
Try this with the variable "input" as the UTF-8 String;
尝试将变量“input”作为 UTF-8 字符串;
VB.NET:
VB.NET:
Dim result As Byte() = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), input);
C#:
C#:
byte[] result = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), input);