4 回答
As noted elsewhere in the responses there is no per-mille sign in ISO 8859-1.
However you can send is as an entity (‰
). As the underlying model of HTML (like XML) is Unicode, characters that cannot be represented in the text encoding, but are in Unicode, can be included as entities.
The character '‰'
is not in ISO-8859-1. However, it is in codepage 1252 which is often confused with ISO-8859-1.
The best solution is to use ‰
as Richard suggests, and/or to change from ISO-8859-1 to UTF-8.
(There's also the possibilty to switch to codepage 1252 (Windows-1252
), but there's a risk it won't work for all users. That would be charset="windows‑1252"
.)
public static string stringToISO8859str(string textToConvert)
{
Encoding iso8859 = Encoding.GetEncoding("iso-8859-1");
Encoding unicode = Encoding.Unicode;
byte[] srcTextBytes = iso8859.GetBytes(textToConvert);
byte[] destTextBytes = Encoding.Convert(iso8859, unicode, srcTextBytes);
char[] destChars = new char[unicode.GetCharCount(destTextBytes, 0, destTextBytes.Length)];
unicode.GetChars(destTextBytes, 0, destTextBytes.Length, destChars, 0);
StringBuilder result = new StringBuilder(textToConvert.Length + (int)(textToConvert.Length * 0.1));
foreach (char c in destChars)
{
int value = Convert.ToInt32(c);
if (value == 34)
result.AppendFormat(""");
else if (value == 38)
result.AppendFormat("&");
else if (value == 39)
result.AppendFormat("'");
else if (value == 60)
result.AppendFormat("<");
else if (value == 62)
result.AppendFormat(">");
else
result.Append(c);
}
return result.ToString();
}