Base64 decode data

From Rosetta Code
Revision as of 21:16, 9 December 2018 by PureFox (talk | contribs) (→‎{{header|Go}}: Aligned output better.)
Base64 decode data is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

See Base64 encode data.

Now write a program that takes the output of the Base64 encode data task as input and regenerate the original file.

When working on the VBA implementation I found several 'solutions' on the net, including one from the software maker himself, that showed output with incorrect padding. Obviously with incorrect padding in the output you can not decode correctly to the original file again.

Go

As images can no longer be uploaded to RC, I've encoded and decoded a string rather than the Rosetta Code icon. <lang go>package main

import (

   "encoding/base64"
   "fmt"

)

func main() {

   msg := "Rosetta Code Base64 decode data task"
   fmt.Println("Original :", msg)
   encoded := base64.StdEncoding.EncodeToString([]byte(msg))
   fmt.Println("\nEncoded  :", encoded)
   decoded, err := base64.StdEncoding.DecodeString(encoded)
   if err != nil {
       fmt.Println(err)
       return
   }
   fmt.Println("\nDecoded  :", string(decoded))

}</lang>

Output:
Original : Rosetta Code Base64 decode data task

Encoded  : Um9zZXR0YSBDb2RlIEJhc2U2NCBkZWNvZGUgZGF0YSB0YXNr

Decoded  : Rosetta Code Base64 decode data task