Windows console output scrambled characters

When trying out non ASCII utf-8 sequence in println!(), the console outputs scrambled characters.

Code:

fn main() {
    println!("你好,世界!");
}

Output;

浣犲ソ锛屼笘鐣岋紒

When executed after chcp 65001, this gives the correct result. I understand that stdout is also used with redirection and the implementation interprets the console behavior from getting is window and such, as a result this is also not a problem when running directly.

But will happen so when executed in debugging consoles like the one in vscode. Is there any easy solution to the encoding problem?

It sounds like your console is interpreting the strings in a different encoding than utf-8. The easiest would be to configure the terminal to read it with the appropriate encoding, although you could also try to insert a layer in your code that converts the text to some other encoding before writing it to stdout.

1 Like

Note that that will be O(n) in the length of the string that will be trandcoded, which may or may not be acceptable for OP's use case.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.