In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use the Java language to achieve Base64 coding, I hope you will learn something after reading this article, let's discuss it together!
Import java.io.*
Public class MIMEBase64 {
/ *
This is a simple Base64 coding program.
Author: Roc Chen rocanny@163.com
Base64 uses 65 characters from a subset of US-ASCII, each represented by 6 bits
So the Base64 value of "m" is 38 and the binary form is 100110.
For text strings, the encoding process is as follows. For example, "men":
First convert to the US-ASCII value.
"m" decimal 109
"e" decimal 101
"n" decimal 110
Binary:
M 01101101
E 01100101
N 01101110
The three eight bits add up to 24 bits.
011011010110010101101110
And then divided into four six digits.
011011 010110 010101 101110
Now you get four values, in decimal
27 22 21 46
The corresponding Base64 characters are:
B W V u
The encoding is always based on 3 characters, resulting in 4 Base64 characters.
If there are only 1 or 2 characters left, use the special character "=" to complete the 4 words of Base64.
For example, encode "me"
01101101 01100101
0110110101100101
011011 010110 0101
111111 (and, make up 6 digits)
011011 010110 010100
B W U
B W U = ("=" complement 4 characters)
So "bWU=" is the base 64 value of "me".
Another example is the code "m"
01101101
011011 01
111111
011011 010000
B Q = =
So "bQ==" is the Base64 value of "m".
It is worth noting that MIME specifies a maximum of 76 characters per line.
, /
Static String BaseTable [] = {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f"
"g", "h", "I", "j", "k", "l", "m", "n", "o", "p", "Q", "r", "s", "t", "u", "v"
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"
}
Public static void encode (String filename, BufferedWriter out) {
Try {
File f = new File (filename)
FileInputStream fin = new FileInputStream (filename)
/ / read the file to the BYTE array
Byte bytes [] = new byte [(int) (f.length ())]
Int n = fin.read (bytes)
If (n)
< 1) return; // 没有内容 byte buf[] = new byte[4]; // base64 字符数组 int n3byt = n / 3; // 3 bytes 组数 int nrest = n % 3; // 分组后剩余 bytes int k = n3byt * 3; // int linelength = 0; // 行长 int i = 0; // 指针 // 3-bytes 分组 ... while ( i < k ) { buf[0] = (byte)(( bytes[i] & 0xFC) >> 2)
Buf [1] = (byte) ((Bytes [I] & 0x03) > 4))
Buf [2] = (byte) ((Bytes [I + 1] & 0x0F) > 6))
Buf [3] = (byte) (bytes [iTunes 2] & 0x3F)
Send (out, BaseTable [buf [0]])
Send (out, BaseTable [buf [1]])
Send (out, BaseTable [buf [2]])
Send (out, BaseTable [buf [3]])
/ *
The above code can be optimized, but it will be difficult to understand
Buf [0] = (byte) (b [I] > > 2)
Buf [1] = (byte) (b [I] & 0x03) > 4))
Buf [2] = (byte) (b [I + 1] & 0x0F) > 6))
Buf [3] = (byte) (b [I + 2] & 0x3F)
Send (out,BaseTable [buf [0]]) + BaseTable [buf [1]] +
BaseTable [buf [2]] + BaseTable [buf [3]])
, /
If ((linelength + = 4) > = 76) {
Send (out, "")
Linelength = 0
}
I + = 3
}
/ / handle the tail.
If (nrest==2) {
/ / 2 bytes left
Buf [0] = (byte) ((bytes [k] & 0xFC) > > 2)
Buf [1] = (byte) ((Bytes [k] & 0x03) > 4))
Buf [2] = (byte) ((bytes [knot 1] & 0x0F) > 2)
Buf [1] = (byte) ((bytes [k] & 0x03) 0) {
/ / send tail
If ((linelength + = 4) > = 76) send (out, ")
Send (out, BaseTable [buf [0]])
Send (out, BaseTable [buf [1]])
/ /
If (nrest==2) {
Send (out, BaseTable [buf [2]])
}
Else {
Send (out, "=")
}
Send (out, "=")
}
Out.flush ()
/ / the send method used here, please write it yourself according to your needs. It can be to output the results to the console, or to send an email.
}
Catch (Exception e) {
E.printStackTrace ()
}
}
}
After reading this article, I believe you have a certain understanding of "how to implement Base64 coding in Java language". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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: 277
*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.