十六进制解码的字符串和 string.as_bytes() 有什么区别?

问题描述 投票:0回答:1

我正在对字符串进行哈希处理。

let src = String::from("abcd12342020");
let h1 = sha2::sha256::digest(&src.as_bytes());
let h2 = sha2::sha256::digest(hex::decode(&src).expect("this is not hex"));

h1
h2
是不同的。 根本原因是什么?字节表示和十六进制解码有什么区别? (试图提供一个游乐场,但
hex
创建在那里不可用)

string rust binary hex byte
1个回答
0
投票
  • .as_bytes()
    为您提供字符串存储在内存中时的字节序列。
  • hex::decode
    将输入字符串的字节转换为文本表示,例如。您将获得
    "00"
    字节。
    
        
© www.soinside.com 2019 - 2024. All rights reserved.