[英]how to transform label text to UTF-8 in c#
我正在嘗試在C#中將標簽的文本轉換為utf-8,我該怎么辦?
lblName.Text = e.displayName;
您應該查看那里的MSDN示例中提到的解決方案: https : //msdn.microsoft.com/en-us/library/system.text.utf8encoding(v=vs.90).aspx
using System;
using System.Text;
class UTF8EncodingExample {
public static void Main() {
// Create a UTF-8 encoding.
UTF8Encoding utf8 = new UTF8Encoding();
// A Unicode string with two characters outside an 8-bit code range.
String unicodeString =
"This unicode string contains two characters " +
"with codes outside an 8-bit code range, " +
"Pi (\u03a0) and Sigma (\u03a3).";
Console.WriteLine("Original string:");
Console.WriteLine(unicodeString);
// Encode the string.
Byte[] encodedBytes = utf8.GetBytes(unicodeString);
Console.WriteLine();
Console.WriteLine("Encoded bytes:");
foreach (Byte b in encodedBytes) {
Console.Write("[{0}]", b);
}
Console.WriteLine();
// Decode bytes back to string.
// Notice Pi and Sigma characters are still present.
String decodedString = utf8.GetString(encodedBytes);
Console.WriteLine();
Console.WriteLine("Decoded bytes:");
Console.WriteLine(decodedString);
}}
您不能更改字符串的編碼。 在.NET中,字符串是使用UTF-16內部編碼的。 僅當將字符串轉換為字節序列或從字節序列轉換時(例如,寫入文件/從文件讀取),才可以影響編碼。
在C#中,String使用UTF-16。 因此,您可能對此沒有問題。
我認為您的html字符集有問題。
要將頁面字符集設置為“ UTF-8”,請在頁面標題上設置以下內容
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.