Need assistance with Base64 (encoding, decoding)

Hello all,

Currently building a little command-line tool, that will encode/decode strings in base64. Using the Base64 crate, the encode/decode functionality works fantastic out of the box. But how can I decode a base64 string completely to ASCII text? Currently it passes me back a Vec, and I have researched online a couple different ways, but all I can get is the base64 data wrapped in quotesXD.

Here is output from my terminal:

C:\Users\XXXXX\IdeaProjects\Rust\XXXX[master +4 ~0 -0 | +0 ~3 -0 !]> cargo run -- fb 'aGVsbG8gd29ybGQ='
   Compiling XXXXv0.1.0 (C:\Users\XXXX\IdeaProjects\Rust\XXXX)
    Finished dev [unoptimized + debuginfo] target(s) in 0.34s                                                                                                                                                                    
     Running `target\debug\XXXX.exe fb aGVsbG8gd29ybGQ=`
[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
C:\Users\XXXX\IdeaProjects\Rust\XXXXX[master +4 ~0 -0 | +0 ~3 -0 !]> cargo run -- fb 'aGVsbG8gd29ybGQ='
   Compiling XXXXXv0.1.0 (C:\Users\XXXXXX\IdeaProjects\Rust\XXXXX)
    Finished dev [unoptimized + debuginfo] target(s) in 0.33s                                                                                                                                                                    
     Running `target\debug\XXXX.exe fb aGVsbG8gd29ybGQ=`
"aGVsbG8gd29ybGQ="

The first output is the basic usage of the result of decoding using the packages function. The second is the result of saving the decode functions parameters as a vec and then attempting to use the encode function to take the vec and print out the ASCII characters. Probably not doing this correct XD, any help would be greatly appreciate it. The end goal is I want to pass a base64 encoded string, and get the ASCII representation of that data, for example, the base64 encoded data in the above outputs is "hello world". I want to get "hello world" back if I pass the encoded string to the tool.

Here is code for this particular part:

            let data = &decode(_command.parameters).unwrap()[..];
            println!("{:?}", encode(&data));

Appreciate you all

You are decoding and then encoding the data again, so these two operations cancel out. What else do you expect from this, other than getting back the original? You probably meant to just leave off the second encode() completely.

The output is just the debug representation of a string, which is the base64 you've round-tripped through decoding and encoding. (The quotes are because of the debug.)

To get the decoded text from Vec<u8> into String form, use String::from_utf8.

Playground.

1 Like

Yeah, I found someone online that said that encoding it once its in Vec form, would encode the bytes into ASCII string. They were wrong lol.

Awesome, thank you for this. Definitely enjoy seeing other peoples code for "similar" functionality to see what they would have done. Makes me remember that I still have a long way to go to fully work with Rust XD. Thank you again for the response!

It does encode it, but the point is that you don't need that. ASCII is just bytes, so if you have a Vec<u8> or [u8] containing ASCII-interpretable bytes, you should just convert it directly to a string using str::from_utf8() or String::from_utf8().

1 Like

ASCII is a 7bit code, even though typically stored in a trivial 8-bit encoding (the encoding is simply "the high bit is always 0"). The point of Base64 is to encode arbitrary byte data into printable ASCII (for embedding in text protocols, e.g. attach an image in an email). This is presumably what they meant by "encoding [results in an] ASCII string".

When you decode you'll get the original bytes, which could be anything. If it's not UTF8, from_utf8 will return an Err.

1 Like