Skip to content

Instantly share code, notes, and snippets.

@debuggor
Created August 4, 2020 02:12
Show Gist options
  • Save debuggor/38e8ed6cc9924bd239b8ce1d7887d9b8 to your computer and use it in GitHub Desktop.
Save debuggor/38e8ed6cc9924bd239b8ce1d7887d9b8 to your computer and use it in GitHub Desktop.
rust notebook

// 引用依赖
[dev-dependencies]  
hex = "0.3.2"

// 打印bytes
let xx = format!("{}", hex::encode(&key[..]));
println!("x: {}",xx);

println!("===============================");

let result = hex::decode("").unwrap();


bytes转hex


use alloc::string::String;
use alloc::vec::Vec;

static CHARS: &'static[u8] = b"0123456789abcdef";

fn to_hex(dest: &[u8]) -> String {
    let mut v = Vec::with_capacity(&dest.len() * 2);
    for &byte in dest.iter() {
        v.push(CHARS[(byte >> 4) as usize]);
        v.push(CHARS[(byte & 0xf) as usize]);
    }

    unsafe {
        String::from_utf8_unchecked(v)
    }
}



let hex = to_hex(&r.to_bytes());
println!("hex {}", hex);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment