In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about the principle and use of Base64 coding in Go language, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Preface
When passing parameters in the network, we often encode the parameters in Base64, so how to encode Base64 in Go language? What is the principle of Base64 coding?
Go Base64 coding
Standard Base64 coding
/ / Standard Base64 encoding src: = "hello world" res: = base64.StdEncoding.EncodeToString ([] byte (src)) fmt.Println (res) / / aGVsbG8gd29ybGQ= / / Standard Base64 decoding s, err: = base64.StdEncoding.DecodeString (res) fmt.Println (string (s), err) / / hello world
Base64 URL coding
After standard Base64 coding, there will be a'+ 'sign, and during HTTP URL transmission, the' + 'sign will be parsed into spaces, so that the data received by the server is inconsistent with the transmitted data. As a result, the Base64 URL code is derived, which turns'+ 'into'-'for easy transmission in URL.
Therefore, if you want to transfer the encoded data in HTTP URL, you should use this encoding.
/ / Base64 URL encoding src: = "information" res: = base64.URLEncoding.EncodeToString ([] byte (src)) fmt.Println (res) / / 5L-h6oGv / / Base64 URL decoding s, err: = base64.URLEncoding.DecodeString (res) fmt.Println (string (s), err) / / Information what is Base64 encoding
Base64 is one of the most common coding methods used to transmit 8Bit bytecode on the network. It is a method to represent binary data based on 64 printable characters. Base64 coding is a process from binary to character, which can be used to transmit long identification information in HTTP environment. Base64 coding is unreadable and needs to be decoded before reading. Baidu encyclopedia
Through the introduction of Baidu encyclopedia, we can get the following information:
Base64 is an encoding method that encodes binaries into characters
The Base64 encoded character range is 64 printable characters
The encoded result is unreadable and needs to be decoded: that is, Base64 is not an encryption method, but an encoding method, which is translated from one language to another. Of course, it can also be translated back.
Why do you need Base64 coding
We all know that the world of computers is made up of 0 and 1, so how does the combination of 0 and 1 represent real language? this gives rise to the ASCII code. ASCII is designed by the United States, with a total of 128characters, in which it is specified that 0-31st and 127th (33 in total) are control characters or communication-specific characters, and 32 / 126 (95 in total) are printable characters. For example, 10 (0000 1001) indicates line wrapping and 65 (0100 0001) indicates the capital letter A.
Many of the original protocols were based on ASCII and could only transmit printable characters. For example, SMTP (simple Mail transfer Protocol), which is used to send e-mail, can only transfer plain text at first, and does not support binary files such as pictures and voice very well.
So what do we want to do to transmit the data that contains non-printable characters? one way is to design an encoding method to convert the non-printable characters into printable characters, so that the data can be transmitted and received, and then decoded back to get the original data. Base64 is designed to solve this problem.
For the current protocols, binary files are all processed, but we can not guarantee that there will be no problems in the process of network transmission. When files containing non-printable characters are transmitted in the network, they often go through multiple routing devices, because different devices (especially the old routing devices) handle the byte stream in some different ways. in this way, those non-printable bytes may be mishandled, which is not conducive to data transmission. So first encode the data and turn it all into visible bytes to ensure reliable data transmission.
Base64 coding principle
Base64 is called Base64 because it is based on 64 printable characters, which are a subset of printable characters in ASCII coding, including 26 letter capitalization, 10 Arabic numerals,'+ 'sign and' / 'sign. (in fact, there is a'= 'sign for the suffix.)
Coding step
Since there are a total of 64 characters, it is sufficient to use 6 bits (2 to the 6th power = 64), while ASCII codes use 8 bits to represent a character, and 6 and 8 have a minimum common multiple of 24. 3 ASCII code characters can be encoded into 4 Base64 characters. Therefore, the coding process of Base64 is as follows:
Divide the original data into a group of 24 bits every three bytes.
24 bits were divided into 4 groups with 6 bits in each group.
Calculate the decimal value of each group, and get the printable string according to the coding comparison table.
Give a demonstration of the example given on Wikipedia:
Given the word 'Man', there are three bytes.
According to the ASCII code, 24 bits are obtained.
Six groups, divided into four groups
The decimal values of each group are calculated to be 19, 22, 5 and 46 respectively. Compared with the Base64 coding table, the printable string is' TWFu'.
It should be noted that after using Base64 encoding, 3 bytes will be converted to 4 bytes, and the data that needs to be transmitted after encoding is 1 more than before.
Insufficient number of digits
There are two situations when the number of bits is insufficient: two bytes or one byte of data.
If it is two bytes, that is, 16 bits, you need to add two zeros to get three Base64-encoded characters, and finally add a'='to supplement
If it is a byte, that is, 8 bits, you need to add 4 zeros to get two Base64-encoded characters, and finally add two'='to supplement.
Base64 decoding principle
Decoding is the reverse operation of encoding:
Remove the'= 'sign at the end
According to the Base64 coding table, find the corresponding coding value for each character.
Take the last 6 bits of each coded value to form a binary string
For the above binary strings, each 8 forms a byte. If the last set is less than 8, all must be zeros. Discard them.
What you get at this time is the binary encoding of the original data, which is then decoded according to the encoding method (such as ASCII).
For example, decode the above 'code value of Ma'' TWE=':
Get rid of'= 'and get' TWE'
'There are 19 kinds of trichotomies.'22 pas. 22's.
010011 010110 000100
01001101 01100001
M a
Base64 standard coding variant
We sometimes pass the Base64 encoded string and the parameters in the current HTTP URL, but the standard Base64 is not suitable for direct transmission in URL, because for the'+ 'sign, the url encode will be used as a space by encode. For example, the encoded value is' ab+cd', but becomes'ab cd', after using url encode, then the other party also receives'ab cd', 'and decoding must have failed at this time.
To solve this problem, an improved Base64 coding for URL is derived, which not only removes the filled'= 'sign at the end, but also changes the "+" and "/" in the standard Base64 to "-" and "_", respectively.
After reading the above, do you have any further understanding of the principle and use of Base64 coding in the Go language? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.