Outputing .ppm to utf-8 encoding

#1

I am learning Raytracing from this site -> https://rushtonality.com/2019/01/25/my-tour-of-rust-day-3-ray-tracing-part-1/

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)

0 Likes

#2

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

#3

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

#4

Thanks for your answers, what i did is entered chcp 65001 windows terminal then it start using utf-8 encoding which worked.(https://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8)

0 Likes