2

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

ただし、バイトをデコードしてテキストに戻す代わりに、数値の文字列として返すだけです。これについてはあまり経験がないので、何が間違っているのかわかりません。

編集:私が達成しようとしていることを明確にするために。文字/数値ではなく、テキストをバイトとして保存するテキスト ファイルを作成しようとしています。これは、ファイルに非常に単純な暗号化を提供するためです。そのため、何をしているのかわからない限り、ファイルを変更することはできません。次に、Decode 関数を使用して、ファイルからテキスト (バイト) を読み取り、読み取り可能なテキストにします。これが私が達成しようとしていることを明確にしたことを願っています。

PS: コメントがないので申し訳ありませんが、理解できるほど短いと思います

4

3 に答える 3

4

正確に何を達成しようとしていますか?UTF-8 (および他のすべてのEncodings) は、文字列をバイト配列 (テキストから生データ) に、またはその逆に変換する方法です。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()));
    }
}
于 2012-09-27T15:08:32.640 に答える
0

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);
}
于 2012-09-27T15:03:19.667 に答える
0

このコードは、暗号化された文字列をテキストにデコードします。私の側では機能しました。

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;
        }
    }
于 2012-09-27T14:53:24.560 に答える