### 文字列の変換
fn main() {
let text = "Hello, Rust!";
let binary = text.as_bytes();
println!("binary: {:?}", binary);
}
binary: [72, 101, 108, 108, 111, 44, 32, 82, 117, 115, 116, 33]
### テキストファイルの変換
input.txt
Hello Rust!
This is sample
use std::fs::File;
use std::io::{self, Read, Write};
fn main() -> io::Result<()>{
let mut input_file = File::open("./data/input.txt")?;
let mut buffer = Vec::new();
input_file.read_to_end(&mut buffer)?;
println!("binary: {:?}", &buffer);
Ok(())
}
### データのchunk
fn main(){
let data = vec![0u8; 10000];
let chunk_size = 1024;
for(i, chunk) in data.chunks(chunk_size).enumerate() {
println!("チャンク {}: サイズ = {}", i, chunk.len());
}
}
チャンク 0: サイズ = 1024
チャンク 1: サイズ = 1024
チャンク 2: サイズ = 1024
チャンク 3: サイズ = 1024
チャンク 4: サイズ = 1024
チャンク 5: サイズ = 1024
チャンク 6: サイズ = 1024
チャンク 7: サイズ = 1024
チャンク 8: サイズ = 1024
チャンク 9: サイズ = 784
### 画像データのchunk
use std::fs::File;
use std::io::{self, Read, Write};
fn main()-> io::Result<()>{
let mut input_file = File::open("./data/test.jpg")?;
let mut buffer = Vec::new();
input_file.read_to_end(&mut buffer)?;
let chunk_size = 1024;
for(i, chunk) in buffer.chunks(chunk_size).enumerate() {
println!("チャンク {}: 値 = {:?}", i, chunk);
}
Ok(())
}