Base64转码和字符串互转
作者:互联网
Base64(基底64)是一种基于64个可打印字符来表示二进制数据的表示方法。一些如uuencode的其他编码方法,和之后BinHex的版本使用不同的64字符集来代表6个二进制数字,但是不被称为Base64。
Base64常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据,包括MIME的电子邮件及XML的一些复杂数据。
但是目前我的代码只能完成字符串和Base64之间的转换!
代码如下:
方法一:
package Day1_4; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * @author : [17151] * @version : [v1.0] * @description : [将字符串转换成base64] * @createTime : [2022/1/4 16:43] * @updateUser : [17151] * @updateTime : [2022/1/4 16:43] * @updateRemark : [说明本次修改内容] * 1、先及那个字符串转换成二进制码 * 2、再将二进制码六个为一组存到数组中 * 3、将数组中的二进制转化成base64格式 * 4、最后输出结果 */ public class Base64 { public static void main(String[] args) { String str = "奥里给"; //System.out.println(to2(str)); //System.out.println(save(str)); System.out.println(toBase64(str)); System.out.println(new String(java.util.Base64.getEncoder().encode(str.getBytes()))); } //将字符串转换成二进制 public static String to2(String str) { byte[] bytes = str.getBytes(); System.out.println(bytes.length); String result = ""; for (byte b : bytes) { result += getBinaryStrFromByte(b); } System.out.println(result.length()); if ((result.length() % 6) != 0) { for (int i = 0; i < (result.length() % 6); i++) { result += 0; } } return result; } public static String getBinaryStrFromByte(byte b) { String result = ""; byte a = b; for (int i = 0; i < 8; i++) { byte c = a; a = (byte) (a >> 1);//每移一位如同将10进制数除以2并去掉余数。 a = (byte) (a << 1); if (a == c) { result = "0" + result; } else { result = "1" + result; } a = (byte) (a >> 1); } return result; } //将二进制以每六位储存 public static List save(String str) { String result = to2(str); List<String> list = new ArrayList<>(); for (int i = 0; i < result.length(); i += 6) { list.add(result.substring(i, i + 6)); } return list; } //将十进制转换成base64 public static String toBase64(String str) { List<String> list1 = save(str); char[] ch = new char[]{'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', 'g', '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', '+', '/'}; String newstr = ""; for (String li : list1) { Integer num = Integer.parseInt(li, 2); newstr = newstr + ch[num]; } return newstr; } }
方法二:比方法一简洁很多
package Day1_5; public class Base2 { static char []ch = new char[]{'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','g','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 main(String[] args) { String org = "奥里给"; toto(org); System.out.println(new String(java.util.Base64.getEncoder().encode(org.getBytes()))); } public static void toto(String str){ byte[] bytes = str.getBytes(); System.out.println(bytes.length); String newstr = ""; int ys = bytes.length % 3;//余数 for (int i = 0; i < bytes.length-ys; i+=3) { int v = ((bytes[i] & 0xFF) << 16) | ((bytes[i+1] & 0xFF) << 8) |(bytes[i+2] & 0xFF); int d3 = v & 0x3F; int d2 = v >>> 6 & 0x3F; int d1 = v >>> 12 & 0x3F; int d0 = v >>> 18 & 0x3F; newstr = newstr+ch[d0]+ch[d1]+ch[d2]+ch[d3]; } if(ys==1){ byte b = bytes[bytes.length-1]; newstr += ch[(b & 0xFF) >>> 2]; newstr += ch[((b & 0xFF) << 4 & 0x3F)]; newstr += "=="; }else if(ys==2){ int b = ((bytes[bytes.length-2] & 0xFF) << 8) | (bytes[bytes.length-1] & 0xFF); newstr += ch[b >>> 10 & 0x3F]; newstr += ch[b >>> 4 & 0x3F]; newstr += ch[b << 2 & 0x3F]; newstr += "="; } System.out.println(newstr); } }
标签:ch,String,转码,Base64,bytes,result,str,互转,newstr 来源: https://blog.csdn.net/m0_51715021/article/details/122352546