Pure Programmer
Blue Matrix


Cluster Map

Project: Base64 Encoding

[[Base64]] is a binary-to-text encoding scheme used in the early days of computing when SMTP (email) and other network protocols only supported 7-bit bytes. It is still used today for attachments to email files. The idea is to convert a file that uses 8-bit bytes into a stream of 7-bit ASCII characters. By using a radix of 64 we can represent 6-bit values with one of the characters A-Z, a-z, 0-9, +, /. So the idea is to break three octets up into four sextets, then convert the sextet to one of our 64 7-bit safe ASCII characters. Depending on the length of the data one or two equal signs '=' will need to be output at the end to pad the output to an even multiple of four. Also to make things easier to read, a newline is output after every 76 output characters.

Write a program that accepts a filename on the command line, converts the file to Base64 and outputs it to the console.

See Base64Decoding

Output
$ rustc Base64Encoding.rs error[E0425]: cannot find value `ifh` in this scope --> Base64Encoding.rs:76:10 | 76 | binmode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Encoding.rs:78:18 | 78 | while getbyte(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Encoding.rs:82:8 | 82 | close(ifh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> Base64Encoding.rs:89:3 | 89 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | warning: unnecessary parentheses around assigned value --> Base64Encoding.rs:26:18 | 26 | sixBitCode = (leftoverBits << 4); | ^ ^ | = note: `#[warn(unused_parens)]` on by default help: remove these parentheses | 26 - sixBitCode = (leftoverBits << 4); 26 + sixBitCode = leftoverBits << 4; | warning: unnecessary parentheses around assigned value --> Base64Encoding.rs:31:18 | 31 | sixBitCode = (leftoverBits << 2); | ^ ^ | help: remove these parentheses | 31 - sixBitCode = (leftoverBits << 2); 31 + sixBitCode = leftoverBits << 2; | error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:27:30 | 27 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:32:30 | 32 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:50:29 | 50 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:56:29 | 56 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:61:29 | 61 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:62:29 | 62 | print!("{}", base64Codes[leftoverBits]); | ^^^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0425]: cannot find function `binmode` in this scope --> Base64Encoding.rs:76:2 | 76 | binmode(ifh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getbyte` in this scope --> Base64Encoding.rs:78:8 | 78 | while getbyte(c,ifh) { | ^^^^^^^ not found in this scope error[E0308]: mismatched types --> Base64Encoding.rs:79:16 | 79 | outputBase64(c); | ------------ ^ expected `isize`, found `u8` | | | arguments to this function are incorrect | note: function defined here --> Base64Encoding.rs:19:4 | 19 | fn outputBase64(c:isize) -> () { | ^^^^^^^^^^^^ ------- help: you can convert a `u8` to an `isize` | 79 | outputBase64(c.into()); | +++++++ error[E0425]: cannot find function `close` in this scope --> Base64Encoding.rs:82:2 | 82 | close(ifh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> Base64Encoding.rs:75:40 | 75 | fn convertBinaryFile(filespec:&str) -> Result<(), Arc<dyn error::Error>> { | ----------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0433]: failed to resolve: use of undeclared crate or module `io` --> Base64Encoding.rs:98:59 | 98 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 16 previous errors; 2 warnings emitted Some errors have detailed explanations: E0277, E0308, E0425, E0433. For more information about an error, try `rustc --explain E0277`. $ rustc Base64Encoding.rs error[E0425]: cannot find value `ifh` in this scope --> Base64Encoding.rs:76:10 | 76 | binmode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Encoding.rs:78:18 | 78 | while getbyte(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Encoding.rs:82:8 | 82 | close(ifh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> Base64Encoding.rs:89:3 | 89 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | warning: unnecessary parentheses around assigned value --> Base64Encoding.rs:26:18 | 26 | sixBitCode = (leftoverBits << 4); | ^ ^ | = note: `#[warn(unused_parens)]` on by default help: remove these parentheses | 26 - sixBitCode = (leftoverBits << 4); 26 + sixBitCode = leftoverBits << 4; | warning: unnecessary parentheses around assigned value --> Base64Encoding.rs:31:18 | 31 | sixBitCode = (leftoverBits << 2); | ^ ^ | help: remove these parentheses | 31 - sixBitCode = (leftoverBits << 2); 31 + sixBitCode = leftoverBits << 2; | error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:27:30 | 27 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:32:30 | 32 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:50:29 | 50 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:56:29 | 56 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:61:29 | 61 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:62:29 | 62 | print!("{}", base64Codes[leftoverBits]); | ^^^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0425]: cannot find function `binmode` in this scope --> Base64Encoding.rs:76:2 | 76 | binmode(ifh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getbyte` in this scope --> Base64Encoding.rs:78:8 | 78 | while getbyte(c,ifh) { | ^^^^^^^ not found in this scope error[E0308]: mismatched types --> Base64Encoding.rs:79:16 | 79 | outputBase64(c); | ------------ ^ expected `isize`, found `u8` | | | arguments to this function are incorrect | note: function defined here --> Base64Encoding.rs:19:4 | 19 | fn outputBase64(c:isize) -> () { | ^^^^^^^^^^^^ ------- help: you can convert a `u8` to an `isize` | 79 | outputBase64(c.into()); | +++++++ error[E0425]: cannot find function `close` in this scope --> Base64Encoding.rs:82:2 | 82 | close(ifh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> Base64Encoding.rs:75:40 | 75 | fn convertBinaryFile(filespec:&str) -> Result<(), Arc<dyn error::Error>> { | ----------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0433]: failed to resolve: use of undeclared crate or module `io` --> Base64Encoding.rs:98:59 | 98 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 16 previous errors; 2 warnings emitted Some errors have detailed explanations: E0277, E0308, E0425, E0433. For more information about an error, try `rustc --explain E0277`. $ rustc Base64Encoding.rs error[E0425]: cannot find value `ifh` in this scope --> Base64Encoding.rs:76:10 | 76 | binmode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Encoding.rs:78:18 | 78 | while getbyte(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Encoding.rs:82:8 | 82 | close(ifh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> Base64Encoding.rs:89:3 | 89 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | warning: unnecessary parentheses around assigned value --> Base64Encoding.rs:26:18 | 26 | sixBitCode = (leftoverBits << 4); | ^ ^ | = note: `#[warn(unused_parens)]` on by default help: remove these parentheses | 26 - sixBitCode = (leftoverBits << 4); 26 + sixBitCode = leftoverBits << 4; | warning: unnecessary parentheses around assigned value --> Base64Encoding.rs:31:18 | 31 | sixBitCode = (leftoverBits << 2); | ^ ^ | help: remove these parentheses | 31 - sixBitCode = (leftoverBits << 2); 31 + sixBitCode = leftoverBits << 2; | error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:27:30 | 27 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:32:30 | 32 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:50:29 | 50 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:56:29 | 56 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:61:29 | 61 | print!("{}", base64Codes[sixBitCode]); | ^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0277]: the type `str` cannot be indexed by `isize` --> Base64Encoding.rs:62:29 | 62 | print!("{}", base64Codes[leftoverBits]); | ^^^^^^^^^^^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0425]: cannot find function `binmode` in this scope --> Base64Encoding.rs:76:2 | 76 | binmode(ifh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getbyte` in this scope --> Base64Encoding.rs:78:8 | 78 | while getbyte(c,ifh) { | ^^^^^^^ not found in this scope error[E0308]: mismatched types --> Base64Encoding.rs:79:16 | 79 | outputBase64(c); | ------------ ^ expected `isize`, found `u8` | | | arguments to this function are incorrect | note: function defined here --> Base64Encoding.rs:19:4 | 19 | fn outputBase64(c:isize) -> () { | ^^^^^^^^^^^^ ------- help: you can convert a `u8` to an `isize` | 79 | outputBase64(c.into()); | +++++++ error[E0425]: cannot find function `close` in this scope --> Base64Encoding.rs:82:2 | 82 | close(ifh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> Base64Encoding.rs:75:40 | 75 | fn convertBinaryFile(filespec:&str) -> Result<(), Arc<dyn error::Error>> { | ----------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0433]: failed to resolve: use of undeclared crate or module `io` --> Base64Encoding.rs:98:59 | 98 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 16 previous errors; 2 warnings emitted Some errors have detailed explanations: E0277, E0308, E0425, E0433. For more information about an error, try `rustc --explain E0277`.

Solution