Adam
Adam

Reputation: 31

Why is StreamWriter writing crosses and Chinese characters?

I have a little script that converts a pixel art image into a string, using 'X' for opaque pixels, ' ' for transparent pixels, and '\n' after each row. I want to then write this string to a text file, which seems simple enough.

    [SerializeField] Sprite sprite;
    [SerializeField] string newFileName;
    private string textFilesFolderLocation = //A location on my computer.
    private string fileExtension = ".txt";

    public void CreateTextDocument() {
        string ascii = CreateAscii();
        
        string path = textFilesFolderLocation + @"\" + newFileName + fileExtension;
        if (File.Exists(path)) {
            File.Delete(path);
        }

        using (StreamWriter streamWriter = new StreamWriter(path)) {
            streamWriter.Write(ascii);
        }
    }
private string CreateAscii() {
        StringBuilder stringBuilder = new StringBuilder();
        Texture2D pixels = sprite.texture;
        for (int y = pixels.height - 1; y >= 0; y--) {
            for (int x = 0; x < pixels.width; x++) {
                Color pixel = pixels.GetPixel(x, y);
                if (pixel.a > 0) {
                    stringBuilder.Append("x");
                }
                else {
                    stringBuilder.Append(" ");
                }
            }
            stringBuilder.Append('\n');
        }

        return stringBuilder.ToString();
    }

This works as expected with ~90% of my images, but a few are writing some seemingly bizarre characters to their text files! Variations of the following:

"...††††††††††††††††††††††††††††††††††††††††††††††††††
††††††††††††††††††††††††††††††††††††††††††††††††砠†
⁸††††††††砠†硸††††††††††††††††††††††††††††††砠†††††
††††砠†††††砠砠††††††††††††††††††††††††††††⁸††††††††
††††††††††††..." (the full text is around 50,000 characters long)

After investigating, I can confirm that CreateAscii() is working correctly. Printing the string to the console reveals that it is comprised of 'x' and ' ', as expected.

I have found that attempting to write simple messages like "Hello World" works as expected, and that writing a small piece of the ascii string also works. But with the few problematic images, trying to write the whole string results in "...†砠†硸†††...".

Even if I try to write small pieces sequentially like

for (int i = 0; i < ascii.Length; i++) {
            using (StreamWriter streamWriter = new StreamWriter(path, append: true)) {
                streamWriter.Write(ascii[i]);
            }
        }

I still get exclusively "...†砠†硸†††..." in my text file.

Does anybody know what might be going on?

Upvotes: 0

Views: 250

Answers (1)

Adam
Adam

Reputation: 31

Thanks to the helpful replies in the comments, I learned that Notepad was having trouble displaying my files correctly. Opening the .txt files with a different viewer enabled me to see the text properly.

I still don't know what the root problem was, but this is good enough for me!

Upvotes: 0

Related Questions