|
@@ -0,0 +1,147 @@
|
|
|
|
+package com.huimv.manage.controller;
|
|
|
|
+
|
|
|
|
+import com.huimv.manage.util.HexUtils;
|
|
|
|
+import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
+import sun.misc.BASE64Decoder;
|
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
|
+
|
|
|
|
+import javax.xml.bind.DatatypeConverter;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.math.BigInteger;
|
|
|
|
+import java.sql.SQLOutput;
|
|
|
|
+import java.util.Base64;//注意:这是jdk8才有的
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Project : huimv.shiwan
|
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
|
+ * @Description : TODO
|
|
|
|
+ * @Version : 1.0
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Create : 2020-12-25
|
|
|
|
+ **/
|
|
|
|
+@SpringBootTest
|
|
|
|
+public class ByteTest {
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void ByteArrayToBinary(){
|
|
|
|
+// String byte1 = "paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=";
|
|
|
|
+// Byte by1 = new Byte("paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=");
|
|
|
|
+// System.out.println("二进制数据>>");
|
|
|
|
+// String str = System.Text.Encoding.ASCII.GetString ( "paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=" );
|
|
|
|
+// System.out.println("2进制:" + binary(by1, 2));
|
|
|
|
+
|
|
|
|
+ String res = new String("paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=");
|
|
|
|
+ System.out.println("res>>"+res);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testBase64() throws UnsupportedEncodingException {
|
|
|
|
+ String text = "paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=";
|
|
|
|
+ byte[] asBytes = Base64.getDecoder().decode(text);
|
|
|
|
+ System.out.println(new String(asBytes, "utf-8")); //这是Base64编码前的原始数据!!!
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void test2() throws Exception{
|
|
|
|
+ String code_format = "ISO-8859-1";
|
|
|
|
+// String sourceData = "这是Base64编码前的原始数据!!!";
|
|
|
|
+ String sourceData = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
|
|
|
|
+ byte[] bytes = sourceData.getBytes(code_format);
|
|
|
|
+ System.out.println("最初的数据="+sourceData);
|
|
|
|
+
|
|
|
|
+ // 编码
|
|
|
|
+ String asB64 = Base64.getEncoder().encodeToString(bytes);
|
|
|
|
+ System.out.println("编码后数据="+asB64+",length="+asB64.length()); //6L+Z5pivQmFzZTY057yW56CB5YmN55qE5Y6f5aeL5pWw5o2u77yB77yB77yB
|
|
|
|
+ System.out.println("vbYmrvwFiAAwAmADYALYAdgDMAIoB0OSODyqJceg88A=.length="+"paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=".length());
|
|
|
|
+ // 解码
|
|
|
|
+ byte[] asBytes = Base64.getDecoder().decode(asB64);
|
|
|
|
+ System.out.println("解码后数据="+new String(asBytes, code_format)); //这是Base64编码前的原始数据!!!
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void test3() {
|
|
|
|
+ String str = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
|
|
|
|
+ //BASE64编码与解码
|
|
|
|
+ String encode = DatatypeConverter.printBase64Binary(str.getBytes());
|
|
|
|
+ System.out.println("1="+encode);
|
|
|
|
+ byte[] decode= DatatypeConverter.parseBase64Binary(encode);
|
|
|
|
+ System.out.println("2="+new String(decode));
|
|
|
|
+
|
|
|
|
+ //16进制编码与解码
|
|
|
|
+ String encode1 = DatatypeConverter.printHexBinary(str.getBytes());
|
|
|
|
+ System.out.println("3="+encode1);
|
|
|
|
+ byte[] decode1= DatatypeConverter.parseHexBinary(encode1);
|
|
|
|
+ System.out.println("4="+new String(decode1));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void test4() throws IOException {
|
|
|
|
+ String data = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
|
|
|
|
+ BASE64Encoder encoder = new BASE64Encoder();
|
|
|
|
+ String e1 = encoder.encode(data.getBytes("UTF-8"));
|
|
|
|
+ System.out.println("e1="+e1);
|
|
|
|
+
|
|
|
|
+ BASE64Decoder decode = new BASE64Decoder();
|
|
|
|
+ byte[] d1 = decode.decodeBuffer(e1);
|
|
|
|
+ System.out.println("d1="+new String(d1));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static String binary(byte[] bytes, int radix) {
|
|
|
|
+ return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private HexUtils hexUtils;
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void hexUtils(){
|
|
|
|
+ String data = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
|
|
|
|
+
|
|
|
|
+ // byte[] 转换为 16进制字符串
|
|
|
|
+// String hex = HexUtils.bytes2Hex(data.getBytes());
|
|
|
|
+// System.out.println(hex); // 输出: 48656c6c6f20576f726c64
|
|
|
|
+
|
|
|
|
+ String hex = "vbYmrvwFiAAwAmADYALYAdgDMAIoB0OSODyqJceg88A=";
|
|
|
|
+ // 16进制字符串 转换为 byte[]
|
|
|
|
+ byte[] bytes = HexUtils.hex2Bytes(hex);
|
|
|
|
+ System.out.println(new String(bytes)); // 输出: Hello World
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void parse1(){
|
|
|
|
+ String base64_str = "MDQw";
|
|
|
|
+ System.out.println("base64:" + base64_str);
|
|
|
|
+ String hex = DatatypeConverter.printHexBinary(DatatypeConverter.parseBase64Binary(base64_str));
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < hex.length(); i+=6) {
|
|
|
|
+ String bytes = hex.substring(i, i+6);
|
|
|
|
+
|
|
|
|
+ System.out.println("hex: " + bytes);
|
|
|
|
+
|
|
|
|
+ StringBuilder binary = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ int byte3_int = Integer.parseInt(bytes.substring(4, 6), 16);
|
|
|
|
+ String byte3_str = Integer.toBinaryString(byte3_int);
|
|
|
|
+ byte3_int = Integer.valueOf(byte3_str);
|
|
|
|
+ binary.append(String.format("%08d", byte3_int));
|
|
|
|
+
|
|
|
|
+ int byte2_int = Integer.parseInt(bytes.substring(2, 4), 16);
|
|
|
|
+ String byte2_str = Integer.toBinaryString(byte2_int);
|
|
|
|
+ byte2_int = Integer.valueOf(byte2_str);
|
|
|
|
+ binary.append(String.format("%08d", byte2_int));
|
|
|
|
+
|
|
|
|
+ int byte1_int = Integer.parseInt(bytes.substring(0, 2), 16);
|
|
|
|
+ String byte1_str = Integer.toBinaryString(byte1_int);
|
|
|
|
+ byte1_int = Integer.valueOf(byte1_str);
|
|
|
|
+ binary.append(String.format("%08d", byte1_int));
|
|
|
|
+
|
|
|
|
+ System.out.println("binary: " + binary);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|