C# UTF8 解码,返回字节/数字而不是字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12623548/
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
C# UTF8 Decoding, returning bytes/numbers instead of string
提问by PeppeJ
I've having an issue decoding a file using an UTF8Encoder.
我在使用 UTF8Encoder 解码文件时遇到问题。
I am reading text from a file which I have encoded with UTF8 (String > Byte) See the following method.
我正在从我用 UTF8(字符串 > 字节)编码的文件中读取文本,请参阅以下方法。
public static void Encode(string Path)
{
string text;
Byte[] bytes;
using (StreamReader sr = new StreamReader(Path))
{
text = sr.ReadToEnd();
UTF8Encoding Encoding = new UTF8Encoding();
bytes = Encoding.GetBytes(text);
sr.Close();
}
using (StreamWriter sw = new StreamWriter(Path))
{
foreach (byte b in bytes)
sw.Write(b.ToString());
sw.Close();
}
}
I then decode it using the method
然后我使用该方法对其进行解码
public static String Decode(string Path)
{
String text;
Byte[] bytes;
using (StreamReader sr = new StreamReader(Path))
{
text = sr.ReadToEnd();
UTF8Encoding Encoding = new UTF8Encoding();
bytes = Encoding.GetBytes(text);
text = Encoding.GetString(bytes);
return text;
}
}
But instead of decoding the byte to have it come back to text, it just returns it as a string of numbers. I can't see what I am doing wrong as I don't really have much experience with this.
但不是解码字节使其返回文本,它只是将其作为一串数字返回。我看不出我做错了什么,因为我对此没有太多经验。
EDIT: To clarify what I'm trying to achieve. I'm trying to have a text file save the text as bytes, rather than chars/numbers. This is to provide a very simple encryption to the files, that so you can't modify them, unless you know what you're doing. The Decode function is then used to read the text (bytes) from the file and make them in to readable text. I hope this clarified what I'm trying to achieve.
编辑:澄清我想要实现的目标。我试图让一个文本文件将文本保存为字节,而不是字符/数字。这是为文件提供非常简单的加密,因此您无法修改它们,除非您知道自己在做什么。然后使用 Decode 函数从文件中读取文本(字节)并将它们转换为可读文本。我希望这能澄清我想要实现的目标。
PS: Sry for no comments, but I think it's short enough to be understandable
PS:请不要发表评论,但我认为它足够短,可以理解
采纳答案by Mormegil
What exactly are you trying to achieve? UTF-8 (and all other Encodings) is a method to converting strings to byte arrays (text to raw data) and vice versa. StreamReaderand StreamWriterare used to read/write stringsfrom/to files. No need to re-encode anything there. Just using reader.ReadToEnd()will return the correct string.
你到底想达到什么目的?UTF-8(以及所有其他Encodings)是一种将字符串转换为字节数组(文本到原始数据),反之亦然的方法。StreamReader和StreamWriter用于从/向文件读/写字符串。无需在那里重新编码任何内容。仅使用reader.ReadToEnd()将返回正确的字符串。
Your piece of code seems to attempt to write a file containing a list of numbers (as a readable, textual representation) corresponding to UTF-8 bytes of the given text. OK. Even though this is very strange idea (I hope you are not trying to do anything like “encryption” with that.), this is definitely possible, if that's really what you want to do. But you need to separate the readable numbers somehow, e.g. by newlines, and parse it when reading them back:
您的一段代码似乎试图编写一个文件,其中包含与给定文本的 UTF-8 字节对应的数字列表(作为可读的文本表示)。好的。尽管这是一个非常奇怪的想法(我希望你不要试图用它来做“加密”之类的事情。),这绝对是可能的,如果这真的是你想要做的。但是您需要以某种方式分隔可读数字,例如通过换行符,并在读取它们时解析它:
public static void Encode(string path)
{
byte[] bytes;
using (var sr = new StreamReader(path))
{
var text = sr.ReadToEnd();
bytes = Encoding.UTF8.GetBytes(text);
}
using (var sw = new StreamWriter(path))
{
foreach (byte b in bytes)
{
sw.WriteLine(b);
}
}
}
public static void Decode(string path)
{
var data = new List<byte>();
using (var sr = new StreamReader(path))
{
string line;
while((line = sr.ReadLine()) != null)
data.Add(Byte.Parse(line));
}
using (var sw = new StreamWriter(path))
{
sw.Write(Encoding.UTF8.GetString(data.ToArray()));
}
}
回答by Muhammad Aqeel
This code will decode encrypted string to text, it worked on my side.
此代码将加密字符串解码为文本,它在我这边工作。
public static String Decode(string Path)
{
String text;
using (StreamReader sr = new StreamReader(Path))
{
text = st.ReadToEnd();
byte[] bytes = Convert.FromBase64String(text);
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder decoder = encoder.GetDecoder();
int count = decoder.GetCharCount(bytes, 0, bytes.Length);
char[] arr = new char[count];
decoder.GetChars(bytes, 0, bytes.Length, arr, 0);
text= new string(arr);
return text;
}
}
回答by BACON
The StreamReaderclasswill handle decoding for you, so your Decode()method can be as simple as this:
该StreamReader班将处理解码你,所以你的Decode()方法可以很简单,因为这:
public static string Decode(string path)
{
// This StreamReader constructor defaults to UTF-8
using (StreamReader reader = new StreamReader(path))
return reader.ReadToEnd();
}
I'm not sure what your Encode()method is supposed to do, since the intent seems to be to read a file as UTF-8 and then write the text back to the exact same file as UTF-8. Something like this might make more sense:
我不确定您的Encode()方法应该做什么,因为目的似乎是将文件读取为 UTF-8,然后将文本写回与 UTF-8 完全相同的文件。像这样的事情可能更有意义:
public static void Encode(string path, string text)
{
// This StreamWriter constructor defaults to UTF-8
using (StreamWriter writer = new StreamWriter(path))
writer.Write(text);
}

