The difference and connection between Hex and Base32 and base64

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Basic concepts

    • Hex is also called base16, meaning that a binary array is represented by 16 visible characters, and the data size is doubled after encoding because 1 characters need to be represented by 2 visible characters.

    • Base32, meaning to use 32 visible characters to represent a binary array, the encoded data size becomes the original 8/5, that is, 5 characters are represented by 8 visible characters, but the last if less than 8 characters, will be used = to supplement.

    • Base64, which means using 64 visible characters to represent a binary array, the encoded data size becomes the original 4/3, or 3 characters are represented by 4 visible words.

Case

Look at an example:

Package Mainimport "FMT" import "Encoding/hex" import "encoding/base32" import "Encoding/base64" Func main () {s: = "Hello    world! " SB: = []byte (s) hexstring: = Hex. Encodetostring (SB) hexbyte, err: = Hex. Decodestring (hexstring) fmt. PRINTLN (hexstring)//68656c6c6f20776f726c6421 FMT. Println (HexByte, err)//[104 101 108 108 111 + 119 111 [108] <nil> base32stdstring: = Base32. Stdencoding.encodetostring (SB) base32hexstring: = Base32. Hexencoding.encodetostring (SB) base32stdbyte, err1: = Base32. Stdencoding.decodestring (base32stdstring) base32hexbyte, err2: = Base32. Hexencoding.decodestring (base32hexstring) fmt. PRINTLN (base32stdstring)//nbswy3dpeb3w64tmmqqq==== FMT. PRINTLN (base32hexstring)//d1imor3f41rmusjccggg==== FMT. Println (Base32stdbyte, ERR1)//[104 101 108 108 111 119 111] <nil> FMT. Println (Base32hexbyte, ERR2)//[104 101 108 108 111, 119 111, 108] <nil> Base64stdsTring: = base64. Stdencoding.encodetostring (SB) Base64urlstring: = base64. Urlencoding.encodetostring (SB) base64stdbyte, ERR1: = base64. Stdencoding.decodestring (base64stdstring) base64urlbyte, err2: = base64. Urlencoding.decodestring (base64urlstring) fmt. PRINTLN (base64stdstring)//AGVSBG8GD29YBGQH FMT. PRINTLN (base64urlstring)//AGVSBG8GD29YBGQH FMT. Println (Base64stdbyte, ERR1)//[104 101 108 108 111 119 111] <nil> FMT. Println (Base64urlbyte, ERR2)//[104 101 108 108 111 + 119 111 [108] &LT;NIL&GT;}

Usage Scenarios

Some small partners may have questions about why the "Hello world!" is so clear that things become a bunch of things that can not be seen, in fact, this example uses the visible characters, and a lot of characters are not visible, we need to turn these invisible characters into visual characters. So why turn it into a visible character? Are you looking at the garbled character or are you comfortable looking at the visible characters? In fact, not only uncomfortable problems, sometimes you have to use the visible characters, such as the transmission of data in the network, different routers for the processing of invisible characters are different, so there may be data errors, so you must also use the visible characters.

Difference and contact

    • The difference is mainly the difference of space efficiency, Base64 is a relatively high space efficiency. Of course, hex encodings are not case-sensitive, but Base32 and Base64 are differentiated.

    • The connection is that everyone is a stable encoding for changing binary arrays into visible characters.

    • The characters used in stdencoding and hexencoding in Base32 are different, but I don't know the exact other differences.

    • Base64 urlencoding generally used to encode URLs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.