如何在 C# 中将字符串转换为 UTF-8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14057434/
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
How can I transform string to UTF-8 in C#?
提问by Gaara
I have a string that I receive from a third party app and I would like to display it correctly in any language using C# on my Windows Surface.
我有一个从第三方应用程序收到的字符串,我想在 Windows Surface 上使用 C# 以任何语言正确显示它。
Due to incorrect encoding, a piece of my string looks like this in Spanish:
由于编码不正确,我的一段字符串在西班牙语中看起来像这样:
Acci?3n
Acci?3n
whereas it should look like this:
而它应该是这样的:
Acción
行动
According to the answer on this question: How to know string encoding in C#, the encoding I am receiving should be coming on UTF-8 already, but it is read on Encoding.Default (probably ANSI?).
根据这个问题的答案: 如何知道 C# 中的字符串编码,我收到的编码应该已经是 UTF-8 了,但它是在 Encoding.Default(可能是 ANSI?)上读取的。
I am trying to transform this string into real UTF-8, but one of the problems is that I can only see a subset of the Encoding class (UTF8 and Unicode properties only), probably because I'm limited to the windows surface API.
我正在尝试将此字符串转换为真正的 UTF-8,但问题之一是我只能看到 Encoding 类的一个子集(仅限 UTF8 和 Unicode 属性),可能是因为我仅限于 Windows Surface API。
I have tried some snippets I've found on the internet, but none of them have proved successful so far for eastern languages (i.e. korean). One example is as follows:
我已经尝试了一些我在互联网上找到的片段,但到目前为止,对于东方语言(即韩语),没有一个被证明是成功的。一个例子如下:
var utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(myString);
myString= utf8.GetString(utfBytes, 0, utfBytes.Length);
I also tried extracting the string into a byte array and then using UTF8.GetString:
我还尝试将字符串提取到字节数组中,然后使用 UTF8.GetString:
byte[] myByteArray = new byte[myString.Length];
for (int ix = 0; ix < myString.Length; ++ix)
{
char ch = myString[ix];
myByteArray[ix] = (byte) ch;
}
myString = Encoding.UTF8.GetString(myByteArray, 0, myString.Length);
Do you guys have any other ideas that I could try?
你们有什么其他的想法我可以尝试吗?
回答by SLaks
Your code is reading a sequence of UTF8-encoded bytes, and decoding them using an 8-bit encoding.
您的代码正在读取一系列 UTF8 编码的字节,并使用 8 位编码对其进行解码。
You need to fix that code to decode the bytes as UTF8.
您需要修复该代码以将字节解码为 UTF8。
Alternatively (not ideal), you could convert the bad string back to the original byte array—by encoding it using the incorrect encoding—then re-decode the bytes as UTF8.
或者(不理想),您可以将坏字符串转换回原始字节数组——通过使用不正确的编码对其进行编码——然后将字节重新解码为 UTF8。
回答by anothershrubery
As you know the string is coming in as Encoding.Default
you could simply use:
如您所知,字符串正在输入,Encoding.Default
您可以简单地使用:
byte[] bytes = Encoding.Default.GetBytes(myString);
myString = Encoding.UTF8.GetString(bytes);
Another thing you may have to remember: If you are using Console.WriteLine to output some strings, then you should also write Console.OutputEncoding = System.Text.Encoding.UTF8;
!!! Or all utf8 strings will be outputed as gbk...
还有一点你可能要记住:如果你使用 Console.WriteLine 输出一些字符串,那么你也应该写Console.OutputEncoding = System.Text.Encoding.UTF8;
!!!或者所有 utf8 字符串都将输出为 gbk...
回答by MethodMan
string utf8String = "Acci?3n";
string propEncodeString = string.Empty;
byte[] utf8_Bytes = new byte[utf8String.Length];
for (int i = 0; i < utf8String.Length; ++i)
{
utf8_Bytes[i] = (byte)utf8String[i];
}
propEncodeString = Encoding.UTF8.GetString(utf8_Bytes, 0, utf8_Bytes.Length);
Output should look like
输出应该看起来像
Acción
daya?s displays day's
行动
daya?s 显示当天的
call DecodeFromUtf8();
调用 DecodeFromUtf8();
private static void DecodeFromUtf8()
{
string utf8_String = "daya?s";
byte[] bytes = Encoding.Default.GetBytes(utf8_String);
utf8_String = Encoding.UTF8.GetString(bytes);
}
回答by Hassan Fadaie Ghotbie
If you want to save any string to mysql database do this:->
如果要将任何字符串保存到 mysql 数据库,请执行以下操作:->
Your database field structure i phpmyadmin [ or any other control panel] should set to utf8-gerneral-ci
您的数据库字段结构我 phpmyadmin [或任何其他控制面板] 应设置为 utf8-gerneral-ci
2) you should change your string [Ex. textbox1.text] to byte, therefor
2)你应该改变你的字符串[例如。textbox1.text] 到字节,因此
2-1) define byte[] st2;
2-1) 定义 byte[] st2;
2-2) convert your string [textbox1.text] to unicode [ mmultibyte string] by :
2-2) 通过以下方式将您的字符串 [textbox1.text] 转换为 unicode [ mmultibyte string]:
byte[] st2 = System.Text.Encoding.UTF8.GetBytes(textBox1.Text);
3) execute this sql command before any query:
3)在任何查询之前执行此sql命令:
string mysql_query2 = "SET NAMES 'utf8'";
cmd.CommandText = mysql_query2;
cmd.ExecuteNonQuery();
3-2) now you should insert this value in to for example name field by :
3-2)现在您应该将此值插入到例如名称字段中:
cmd.CommandText = "INSERT INTO customer (`name`) values (@name)";
4) the main job that many solution didn't attention to it is the below line: you should use addwithvalue instead of add in command parameter like below:
4)许多解决方案没有注意的主要工作是以下行:您应该使用 addwithvalue 而不是 add in 命令参数,如下所示:
cmd.Parameters.AddWithValue("@name",ut);
++++++++++++++++++++++++++++++++++ enjoy real data in your database server instead of ????
++++++++++++++++++++++++++++++++++ 享受数据库服务器中的真实数据,而不是 ????
回答by jAntoni
Use the below code snippet to get bytes from csv file
使用以下代码片段从 csv 文件中获取字节
protected byte[] GetCSVFileContent(string fileName)
{
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(fileName, Encoding.Default, true))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
}
string allines = sb.ToString();
UTF8Encoding utf8 = new UTF8Encoding();
var preamble = utf8.GetPreamble();
var data = utf8.GetBytes(allines);
return data;
}
Call the below and save it as an attachment
调用下面的并将其另存为附件
Encoding csvEncoding = Encoding.UTF8;
//byte[] csvFile = GetCSVFileContent(FileUpload1.PostedFile.FileName);
byte[] csvFile = GetCSVFileContent("Your_CSV_File_NAme");
string attachment = String.Format("attachment; filename={0}.csv", "uomEncoded");
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "text/csv";
Response.ContentEncoding = csvEncoding;
Response.AppendHeader("Content-Disposition", attachment);
//Response.BinaryWrite(csvEncoding.GetPreamble());
Response.BinaryWrite(csvFile);
Response.Flush();
Response.End();
回答by Riadh Hammouda
Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(mystring));