C#UTF8解码,返回字节/数字而不是字符串 [英] C# UTF8 Decoding, returning bytes/numbers instead of string

查看:166
本文介绍了C#UTF8解码,返回字节/数字而不是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UTF8Encoder解码文件时出现问题。

I've having an issue decoding a file using an UTF8Encoder.

我正在从使用UTF8(String> Byte)编码的文件中读取文本
请参阅以下方法。

I am reading text from a file which I have encoded with UTF8 (String > Byte) See the following method.

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;
        }
    }

但是,不是解码字节让它回来到文本,它只是作为一个数字字符串返回。我看不到我做错了什么,因为我真的没有太多的经验。

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.

编辑:澄清我要实现的目标。我想要一个文本文件将文本保存为字节,而不是字符/数字。这是为文件提供一个非常简单的加密,所以你不能修改它们,除非你知道你在做什么。然后,解码函数用于从文件读取文本(字节),并使其变为可读文本。我希望这澄清了我正在努力实现的目标。

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.

PS:Sry没有任何评论,但我觉得足够简单可以理解

PS: Sry for no comments, but I think it's short enough to be understandable

推荐答案

你究竟想要实现什么? UTF-8(和所有其他 Encoding s)是一种将字符串转换为字节数组(文本到原始数据)的方法,反之亦然。 StreamReader StreamWriter 用于从/向文件读取/写入字符串。不需要重新编码任何东西。只需使用 reader.ReadToEnd()将返回正确的字符串。

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. StreamReader and StreamWriter are used to read/write strings from/to files. No need to re-encode anything there. Just using reader.ReadToEnd() will return the correct string.

您的代码似乎试图写包含对应于给定文本的UTF-8字节的数字列表(作为可读的文本表示)的文件。好。即使这是一个非常奇怪的想法(希望你不要像加密那样做任何事情。),这绝对是可能的,如果这是真的你想做的。但是您需要以某种方式分离可读数字,例如通过换行符,并在读取它们时解析:

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:

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()));
    }
}

这篇关于C#UTF8解码,返回字节/数字而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆