package main
import (
"encoding/base64"
"encoding/hex"
"fmt"
)
func main() {
// Step 1: Convert a string to hexadecimal
str := "Hello, World!"
hexEncoded := hex.EncodeToString([]byte(str))
fmt.Println("Hexadecimal encoding:", hexEncoded)
// Step 2: Convert the hexadecimal string to base64
hexDecoded, err := hex.DecodeString(hexEncoded)
if err != nil {
fmt.Println("Error decoding hex:", err)
return
}
base64Encoded := base64.StdEncoding.EncodeToString(hexDecoded)
fmt.Println("Base64 encoding:", base64Encoded)
// Step 3: Decode the base64 string back to hexadecimal
base64Decoded, err := base64.StdEncoding.DecodeString(base64Encoded)
if err != nil {
fmt.Println("Error decoding base64:", err)
return
}
decodedHex := hex.EncodeToString(base64Decoded)
fmt.Println("Decoded hexadecimal:", decodedHex)
// Step 4: Convert the decoded hexadecimal back to the original string
decodedStr, err := hex.DecodeString(decodedHex)
if err != nil {
fmt.Println("Error decoding hex:", err)
return
}
fmt.Println("Decoded string:", string(decodedStr))
}