In computing, encoding is the process of transforming a sequence of characters (letters, numbers, punctuation, special characters, symbols) into a format that is efficient for transmission and storage. The format commonly used is . It works something like this:
package main
import (
b64 "encoding/base64"
"fmt"
)
func main() {
// Here is the string to be encoded.
data := "abc123!?$*&()'-=@~"
// We performed the encoding.
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(sEnc)
// We performed the decoding.
sDec, _ := b64.StdEncoding.DecodeString(sEnc)
fmt.Println(string(sDec))
}
Output
YWJjMTIzIT8kKiYoKSctPUB+
abc123!?$*&()'-=@~
"Okay, but what will we use this for?" You might be wondering.
Imagine yourself needing to transfer a snippet of code to another computer.
Here's an example:
package main
import (
"fmt"
"net/url"
)
func main() {
s := "this will be esc@ped!"
fmt.Println("http://example.com/say?message="+url.QueryEscape(s))
}
We have new lines, special characters, etc...
Perhaps some information may be lost in the middle of this process depending on the content being transmitted.
So transmitting this value here is better, isn't it?
Base64
cGFja2FnZSBtYWluCgppbXBvcnQgKAogICAgImZtdCIKICAgICJuZXQvdXJsIgopCgpmdW5jIG1haW4oKSB7CiAgICBzIDo9ICJ0aGlzIHdpbGwgYmUgZXNjQHBlZCEiCiAgICBmbXQuUHJpbnRsbigiaHR0cDovL2V4YW1wbGUuY29tL3NheT9tZXNzYWdlPSIrdXJsLlF1ZXJ5RXNjYXBlKHMpKQp9
You can test it yourself with the IT-TOOLS.
Before we move on to the next topic, it's worth remembering:
Encoding is NOT CRYPTOGRAPHY.
Top comments (0)