[英]C# UTF8 Decoding, returning bytes/numbers instead of string
我在使用UTF8Encoder解码文件时遇到问题。
我正在从使用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();
}
}
然后我使用方法解码
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;
}
}
但是,与其解码字节以使其返回文本,不如将其作为数字字符串返回。 我看不到我在做什么错,因为我对此没有太多经验。
编辑:澄清我正在努力实现。 我试图让一个文本文件将文本另存为字节,而不是字符/数字。 这是为文件提供非常简单的加密,因此除非您知道自己在做什么,否则就无法修改它们。 然后,使用解码功能从文件中读取文本(字节)并将其制成可读文本。 我希望这可以澄清我要达到的目标。
PS:很抱歉没有评论,但我认为它很短,可以理解
您到底想达到什么目的? UTF-8(和所有其他Encoding
)是一种将字符串转换为字节数组(从文本转换为原始数据)的方法,反之亦然。 StreamReader
和StreamWriter
用于从文件读取字符串 /向文件写入字符串 。 无需在那里重新编码任何内容。 仅使用reader.ReadToEnd()
将返回正确的字符串。
您的代码似乎试图编写一个文件,其中包含与给定文本的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()));
}
}
这段代码会将加密的字符串解码为文本,它对我有用。
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;
}
}
StreamReader
类将为您处理解码,因此您的Decode()
方法可以像这样简单:
public static string Decode(string path)
{
// This StreamReader constructor defaults to UTF-8
using (StreamReader reader = new StreamReader(path))
return reader.ReadToEnd();
}
我不确定您的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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.