Outputing .ppm to utf-8 encoding

I am learning Raytracing from this site -> My Tour of Rust – Day 3 – Ray Tracing Part 1 – Rushtonality

using this command cargo run --release > image.ppm to output to .ppm format it is encoded in utf-16 which prevent the file from opening, so what should I do to make it output with utf-8 encoding.

(I am fairly new here)

Just a blind guess but is your terminal configured to use UTF-16? Maybe Rust defaults to the terminal's settings when writing strings to stdout, not sure.
Instead of writing a string to stdout using print! (and hoping that the right encoding is chosen) you could encode it to UTF-8 yourself (e.g. using the as_bytes method of String which is guaranteed to be UTF-8 encoded) and then use std::io::Stdout::write:

use std::io::{self, Write};

fn main() {
    let mut stdout = io::stdout();
    stdout.write(format!("Hello {}!", "there").as_bytes()).unwrap();
}
3 Likes

If you're on Windows, and you're using PowerShell, you need to stop using PowerShell. This has nothing to do with Rust.

PowerShell is hard-coded to forcibly translate all text that goes through it into UTF-16, and as far as I can tell, there is no way to change this or turn it off.

I mean, that or change your program to write to a file instead of stdout. That'd work, too.

2 Likes

Thanks for your answers, what i did is entered chcp 65001 windows terminal then it start using utf-8 encoding which worked.(Change default code page of Windows console to UTF-8 - Super User)