C#UTF8解码,返回字节/数字而不是字符串
我在使用UTF8编码器解码文件时遇到问题 我正在从一个用UTF8编码的文件中读取文本(字符串>字节) 请参见以下方法C#UTF8解码,返回字节/数字而不是字符串,c#,encoding,utf-8,io,byte,C#,Encoding,Utf 8,Io,Byte,我在使用UTF8编码器解码文件时遇到问题 我正在从一个用UTF8编码的文件中读取文本(字符串>字节) 请参见以下方法 public static void Encode(string Path) { string text; Byte[] bytes; using (StreamReader sr = new StreamReader(Path)) { text = sr.ReadToEnd();
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;
}
}
但它不是解码字节使其返回文本,而是将其作为一个数字字符串返回。我看不出我做错了什么,因为我在这方面没有太多经验
编辑:澄清我想要达到的目标。我试图让一个文本文件将文本保存为字节,而不是字符/数字。这是为文件提供一个非常简单的加密,这样你就不能修改它们,除非你知道你在做什么。然后使用Decode函数从文件中读取文本(字节),并将其转换为可读文本。我希望这澄清了我想要实现的目标
PS:Sry没有评论,但我认为它足够短,可以理解这段代码将加密字符串解码为文本,对我来说很有效
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;
}
}
这段代码将加密字符串解码为文本,在我这方面很有效
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;
}
}
将为您处理解码,因此您的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);
}
将为您处理解码,因此您的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);
}
你到底想达到什么目的?UTF-8(以及所有其他
编码s)是一种将字符串转换为字节数组(文本转换为原始数据)的方法,反之亦然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()));
}
}
公共静态无效编码(字符串路径)
{
字节[]字节;
使用(var sr=新的StreamReader(路径))
{
var text=sr.ReadToEnd();
字节=Encoding.UTF8.GetBytes(文本);
}
使用(var sw=新StreamWriter(路径))
{
foreach(字节中的字节b)
{
sw.WriteLine(b);
}
}
}
公共静态无效解码(字符串路径)
{
var data=新列表();
使用(var sr=新的StreamReader(路径))
{
弦线;
而((line=sr.ReadLine())!=null)
data.Add(Byte.Parse(line));
}
使用(var sw=新StreamWriter(路径))
{
Write(Encoding.UTF8.GetString(data.ToArray());
}
}
你到底想实现什么?UTF-8(以及所有其他编码s)是一种将字符串转换为字节数组(文本转换为原始数据)的方法,反之亦然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()));
}
}
公共静态无效编码(字符串路径)
{
字节[]字节;
使用(var sr=新的StreamReader(路径))
{
var text=sr.ReadToEnd();
字节=Encoding.UTF8.GetBytes(文本);
}
使用(var sw=新StreamWriter(路径))
{
foreach(字节中的字节b)
{
sw.WriteLine(b);
}
}
}
公共静态无效解码(字符串路径)
{
var data=新列表();
使用(var sr=新的StreamReader(路径))
{
弦线;
而((line=sr.ReadLine())!=null)
data.Add(Byte.Parse(line));
}
使用(var sw=新StreamWriter(路径))
{
Write(Encoding.UTF8.GetString(data.ToArray());
}
}
您正在执行b.ToString()-这不是字节本身,而是字符串表示。尝试使用BinaryWriter?我发现您使用变量名编码
非常令人困惑(而且不是标准的)-正常的C#命名约定是将其作为编码
。当您使用相同的函数调用(Encoding.GetBytes()
)时,您不觉得有问题吗在编码和解码方面?更新了OP以澄清我试图实现的目标,还对变量名进行了排序,我不知道为什么要将它们用大写字母表示。您正在执行b.ToString()-这不是字节本身,而是字符串表示。尝试使用BinaryWriter?我发现您使用变量名编码
非常令人困惑(而且不是标准的)-正常的C#命名约定是将其作为编码
。当您使用相同的函数调用(Encoding.GetBytes()
)时,您不觉得有问题吗在编码和解码方面的事情?更新的OP