我在理解Rust中特徵的概念時遇到了一些問題。我試圖編碼一個簡單的十六進制值轉換爲Base64,但沒有運氣,這裏是我的代碼(有串爲Base64也是一個例子)如何將十六進制值轉換爲Rust中的Base64
extern crate serialize;
use serialize::base64::{ToBase64, STANDARD};
use serialize::hex::{FromHex, ToHex};
fn main() {
let stringOfText = "This is a String";
let mut config = STANDARD;
println!("String to base64 = {}", stringOfText.as_bytes().to_base64(config));
// Can't figure out this
弗拉基米爾提供的解決方案適用於0X譜寫十六進制值。現在,我期待轉換是在一個字符串表示的十六進制值:
extern crate serialize;
use serialize::base64::{ToBase64, STANDARD};
use serialize::hex::{FromHex, ToHex};
fn main() {
let stringOfText = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
let mut config = STANDARD;
println!("String to base64 = {}", stringOfText.from_hex().from_utf8_owned().as_bytes().to_base64(config));
// result should be: SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t
}
from_hex()
給了我一個Vec<u8>
和.to_base64()
期待的u8,
緩衝區開始我還以爲到Vec<u8>
轉換爲字符串,然後使用as_bytes()
得到緩衝區,到目前爲止還是沒有運氣。
的'ToBase64'特質的唯一實施者是'&[U8]'(HTTP:/ /doc.rust-lang.org/serialize/base64/trait.ToBase64.html),因此在使用'to_base64()'之前,您需要將該數字轉換爲它。 – snf 2014-10-03 19:37:44
謝謝@snf,剛剛更新到這個 '讓十六進制= 0x49276d2;' '讓整數=(十六進制)U8;!' '調用println( 「十六進制爲base64 = {}」,integer.to_base64(配置));' 但仍然沒有運氣 – 2014-10-03 19:44:57
我沒有得到你想要做的,你想base64_encode「0x49276d2」或「\ x04 \ x92 \ x76 \ xd2」或其他? – snf 2014-10-04 00:53:19