Base64 decode data: Difference between revisions

From Rosetta Code
Content added Content deleted
(Converted this to a draft task, tidied it up a little and added a Go solution.)
m (→‎{{header|Go}}: Aligned output better.)
Line 20: Line 20:
fmt.Println("Original :", msg)
fmt.Println("Original :", msg)
encoded := base64.StdEncoding.EncodeToString([]byte(msg))
encoded := base64.StdEncoding.EncodeToString([]byte(msg))
fmt.Println("\nEncoded : ", encoded)
fmt.Println("\nEncoded :", encoded)
decoded, err := base64.StdEncoding.DecodeString(encoded)
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
if err != nil {
Line 26: Line 26:
return
return
}
}
fmt.Println("\nDecoded : ", string(decoded))
fmt.Println("\nDecoded :", string(decoded))
}</lang>
}</lang>


Line 33: Line 33:
Original : Rosetta Code Base64 decode data task
Original : Rosetta Code Base64 decode data task


Encoded : Um9zZXR0YSBDb2RlIEJhc2U2NCBkZWNvZGUgZGF0YSB0YXNr
Encoded : Um9zZXR0YSBDb2RlIEJhc2U2NCBkZWNvZGUgZGF0YSB0YXNr


Decoded : Rosetta Code Base64 decode data task
Decoded : Rosetta Code Base64 decode data task
</pre>
</pre>

Revision as of 21:16, 9 December 2018

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