Charsets Problem

Hi:
I tried std::process::Command today.it is good.

  use std::process::Command;

fn main() {
    let output = Command::new("ipconfig")
        .arg("/all")
        .output().unwrap_or_else(|e| {
            panic!("failed to execute process: {}", e)
    });

    if output.status.success() {
        let s = String::from_utf8_lossy(&output.stdout);

        print!("rustc succeeded and stdout was:\n{}", s);
    } else {
        let s = String::from_utf8_lossy(&output.stderr);

        print!("rustc failed and stderr was:\n{}", s);
    }
}

  but I meet a problem with character sets. my Windows system language is not english. I think the cmd character set should be GBK, not utf8. so some results seems as black squares, or gibberish. 
 I also searched for java solution. the Charset.forName can slove that confuse like the code below.
 my questions : is there a function or cfg for command function for that purpose or I should change to another way?:grinning:


public static void main(String[] args) {
		try {
			// 执行ping命令
			Process process = Runtime.getRuntime().exec("cmd /c e:&dir");
			BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
			String line = null;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

If I understand correctly, you want to interpret the bytes that you read from the output of a Command as text with a particular character set. If so, take a look at the encoding_rs crate.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.