拍击原始字节参数

发布于 2025-01-26 17:38:37 字数 1234 浏览 3 评论 0原文

我想将raw byte参数与拍手一起使用,

例如-raw $(echo -n -e'B \ x10cc \ x01 \ xff')会给我以下字节数组[ 66、16、67、67、1、239、191、189](使用to_string_lossy()。to_bytes())。

有没有一种方法可以使用拍手获得精确的字节数组?

输入的编辑

let cmd = Command::new(
        env!("CARGO_CRATE_NAME")
    ).bin_name(
        env!("CARGO_CRATE_NAME")
    ).arg(
        Arg::new("raw").long("raw").takes_value(true).allow_invalid_utf8(true)
    );
    let matches = cmd.get_matches();


    match matches.value_of_os("raw") {
        Some(s) => {
            match s.to_str() {
                Some(s3) => {
                    let v2: &[u8] = s3.as_bytes();
                    println!("OsStr(bytes):{:?}", v2);
                },
                None => {},
            }

            let s2 = s.to_string_lossy();
            println!("string_from_OsString:{}", s2);

            let v3: &[u8] = s2.as_bytes();
            println!("OsString.to_lossy(bytes):{:?}", v3);
        },
        None => {},
    }

返回-raw $(echo -n -e'B \ x10cc \ x01 \ xff')

string_from_OsString:BCC�
OsString.to_lossy(bytes):[66, 16, 67, 67, 1, 239, 191, 189]

谢谢你

I would like to use raw byte argument with clap

For example --raw $(echo -n -e 'B\x10CC\x01\xff') will give me the following bytes array [66, 16, 67, 67, 1, 239, 191, 189] (using to_string_lossy().to_bytes()).

Is there a way to get exact bytes array using clap?

EDIT

let cmd = Command::new(
        env!("CARGO_CRATE_NAME")
    ).bin_name(
        env!("CARGO_CRATE_NAME")
    ).arg(
        Arg::new("raw").long("raw").takes_value(true).allow_invalid_utf8(true)
    );
    let matches = cmd.get_matches();


    match matches.value_of_os("raw") {
        Some(s) => {
            match s.to_str() {
                Some(s3) => {
                    let v2: &[u8] = s3.as_bytes();
                    println!("OsStr(bytes):{:?}", v2);
                },
                None => {},
            }

            let s2 = s.to_string_lossy();
            println!("string_from_OsString:{}", s2);

            let v3: &[u8] = s2.as_bytes();
            println!("OsString.to_lossy(bytes):{:?}", v3);
        },
        None => {},
    }

return for input --raw $(echo -n -e 'B\x10CC\x01\xff')

string_from_OsString:BCC�
OsString.to_lossy(bytes):[66, 16, 67, 67, 1, 239, 191, 189]

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋葬我深情 2025-02-02 17:38:38

拍手是平台不可知论,因此使用osstring之类的摘要(这是您的s变量的类型)。

似乎没有通用as_bytes() osstring的方法,因为不是在每个操作系统上osstring is 实际上是一个原始字节数组。

这是关于这个主题的更多讨论:我可以将OSSTR转换为& [u8]/vec< u8>在Windows上?

因此,为了解决您的问题,似乎有必要将兼容性缩小到特定操作系统。就您而言,似乎您正在使用unix。这很棒,因为对于unix,这样的方法确实存在

在这里您走:

use clap::{Arg, Command};
use std::os::unix::ffi::OsStrExt;

fn main() {
    let cmd = Command::new(env!("CARGO_CRATE_NAME"))
        .bin_name(env!("CARGO_CRATE_NAME"))
        .arg(
            Arg::new("raw")
                .long("raw")
                .takes_value(true)
                .allow_invalid_utf8(true),
        );
    let matches = cmd.get_matches();

    match matches.value_of_os("raw") {
        Some(s) => {
            println!(".as_bytes(): {:?}", s.as_bytes());
        }
        None => {}
    }
}
.as_bytes(): [66, 16, 67, 67, 1, 255]

请注意,使用STD :: OS :: UNIX :: FFI :: OSSTREXT;会将.as_bytes()功能添加到osstring < /code>,但将无法在非Unix系统上编译。

clap is platform agnostic and therefore uses abstractions like OsString (which is the type of your s variable).

There seems to be no generic as_bytes() method attached to OsString, because not on every operating system OsString is actually a raw bytes array.

Here is a lot more discussion about this very topic: How can I convert OsStr to &[u8]/Vec<u8> on Windows?

So to solve your problem, it seems necessary that you narrow your compatibility down to a specific operating system. In your case, it seems that you are using Unix. Which is great, because for Unix, such a method does exist!

Here you go:

use clap::{Arg, Command};
use std::os::unix::ffi::OsStrExt;

fn main() {
    let cmd = Command::new(env!("CARGO_CRATE_NAME"))
        .bin_name(env!("CARGO_CRATE_NAME"))
        .arg(
            Arg::new("raw")
                .long("raw")
                .takes_value(true)
                .allow_invalid_utf8(true),
        );
    let matches = cmd.get_matches();

    match matches.value_of_os("raw") {
        Some(s) => {
            println!(".as_bytes(): {:?}", s.as_bytes());
        }
        None => {}
    }
}
.as_bytes(): [66, 16, 67, 67, 1, 255]

Note that the use std::os::unix::ffi::OsStrExt; will add the .as_bytes() functionality to OsString, but will fail to compile on non-unix systems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文