如题,直接上代码:
fn main() {
let name = String::from("@");
println!("{:?}", getbinary(name));
let work = "青";
println!("{:?}", getbytes(work));
}
//获取字符串对应的二进制
fn getbinary(s:String)-> String{
let mut mybinary = String::from("");
for c in s.clone().into_bytes() {
mybinary += &format!("{:b} ", c);
}
let k:String = mybinary.as_str().trim().to_string();
k
}
//获取字符串对应的字节即十进制
fn getbytes(s:&str) ->&[u8]{
let mybytesvec = s.as_bytes();
mybytesvec
}
//输出内容
"1000000"
[233, 157, 146]