拍击原始字节参数
我想将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
拍手
是平台不可知论,因此使用osstring
之类的摘要(这是您的s
变量的类型)。似乎没有通用
as_bytes()
osstring
的方法,因为不是在每个操作系统上osstring
is 实际上是一个原始字节数组。这是关于这个主题的更多讨论:我可以将OSSTR转换为& [u8]/vec< u8>在Windows上?
因此,为了解决您的问题,似乎有必要将兼容性缩小到特定操作系统。就您而言,似乎您正在使用
unix
。这很棒,因为对于unix
,这样的方法确实存在!在这里您走:
请注意,
使用STD :: OS :: UNIX :: FFI :: OSSTREXT;
会将.as_bytes()
功能添加到osstring < /code>,但将无法在非Unix系统上编译。
clap
is platform agnostic and therefore uses abstractions likeOsString
(which is the type of yours
variable).There seems to be no generic
as_bytes()
method attached toOsString
, because not on every operating systemOsString
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 forUnix
, such a method does exist!Here you go:
Note that the
use std::os::unix::ffi::OsStrExt;
will add the.as_bytes()
functionality toOsString
, but will fail to compile on non-unix systems.