base64ライブラリを書く
alphabetのVecを作る
code:rs
let v = (b'a'..=b'z').map(|c| c as char).collect::<Vec<_>>();
b'a'は[u8]のASCIIバイトリテラル。
(start..=end)はRangeInclusiveという書き方。start <= x <= endの範囲。FusedIteratorを実装している。
collect::<Vec<_>>()でVecへ変換
bytesをbinary stringに変換
code:rs
bytes.iter().fold("".to_owned(), |s, b| format!("{}{:b}", s, b));
binary stringをintegerに変換
code:rust
isize::from_str_radix("100100", 2).unwrap() // => 36
string to XXX
format!のパラメータ
hex to binary
マニュアルで変換する方法が楽そうだった
code:rs
fn convert_hex_char_to_string(&self, c: char) -> String {
match c {
'0' => "0000".to_string(),
'1' => "0001".to_string(),
'2' => "0010".to_string(),
'3' => "0011".to_string(),
'4' => "0100".to_string(),
'5' => "0101".to_string(),
'6' => "0110".to_string(),
'7' => "0111".to_string(),
'8' => "1000".to_string(),
'9' => "1001".to_string(),
'A' => "1010".to_string(),
'B' => "1011".to_string(),
'C' => "1100".to_string(),
'D' => "1101".to_string(),
'E' => "1110".to_string(),
'F' => "1111".to_string(),
_ => "".to_string(),
}
}
Rust公式の実装