|
@@ -14,7 +14,9 @@ import java.io.UnsupportedEncodingException;
|
|
import java.math.BigInteger;
|
|
import java.math.BigInteger;
|
|
import java.sql.SQLOutput;
|
|
import java.sql.SQLOutput;
|
|
import java.util.Base64;//注意:这是jdk8才有的
|
|
import java.util.Base64;//注意:这是jdk8才有的
|
|
|
|
+//import org.apache.commons.codec.binary.Base64;
|
|
|
|
|
|
|
|
+import java.util.Arrays;
|
|
/**
|
|
/**
|
|
* @Project : huimv.shiwan
|
|
* @Project : huimv.shiwan
|
|
* @Package : com.huimv.biosafety.uface.controller
|
|
* @Package : com.huimv.biosafety.uface.controller
|
|
@@ -40,25 +42,33 @@ public class ByteTest {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void testBase64() throws UnsupportedEncodingException {
|
|
public void testBase64() throws UnsupportedEncodingException {
|
|
- String text = "paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=";
|
|
|
|
- byte[] asBytes = Base64.getDecoder().decode(text);
|
|
|
|
- System.out.println(new String(asBytes, "utf-8")); //这是Base64编码前的原始数据!!!
|
|
|
|
|
|
+ String text = "jb7L12gAeAFYAXgBcALAAZgDAAAQAiinnZH+VIu9684=";
|
|
|
|
+//// String text = "119";
|
|
|
|
+// byte[] asBytes = Base64.getDecoder().decode(text);
|
|
|
|
+// System.out.println(new String(asBytes, "utf-8")); //这是Base64编码前的原始数据!!!
|
|
|
|
+// int a=0;
|
|
|
|
+// for(Byte b:asBytes){
|
|
|
|
+// System.out.println((a++)+","+b);
|
|
|
|
+// }
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void test2() throws Exception{
|
|
public void test2() throws Exception{
|
|
- String code_format = "ISO-8859-1";
|
|
|
|
|
|
+ String code_format = "UTF-8";
|
|
// String sourceData = "这是Base64编码前的原始数据!!!";
|
|
// String sourceData = "这是Base64编码前的原始数据!!!";
|
|
String sourceData = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
|
|
String sourceData = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
|
|
|
|
+// String sourceData = "211011500094511";
|
|
byte[] bytes = sourceData.getBytes(code_format);
|
|
byte[] bytes = sourceData.getBytes(code_format);
|
|
- System.out.println("最初的数据="+sourceData);
|
|
|
|
-
|
|
|
|
|
|
+ System.out.println("最初的数据:"+sourceData+",length="+sourceData.length());
|
|
|
|
+ System.out.println("base64编码=YTVhMDg5NGM0YzA2MzgwMzY4MDE3ODAwNjAwMmEwMDFiMDAzNjgwMjIwMDU3NTA1OTIxZTIyMzQ0MWRlOTNjNg==,length=88");
|
|
// 编码
|
|
// 编码
|
|
String asB64 = Base64.getEncoder().encodeToString(bytes);
|
|
String asB64 = Base64.getEncoder().encodeToString(bytes);
|
|
- System.out.println("编码后数据="+asB64+",length="+asB64.length()); //6L+Z5pivQmFzZTY057yW56CB5YmN55qE5Y6f5aeL5pWw5o2u77yB77yB77yB
|
|
|
|
- System.out.println("vbYmrvwFiAAwAmADYALYAdgDMAIoB0OSODyqJceg88A=.length="+"paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=".length());
|
|
|
|
|
|
+// String asB64 = Base64.encodeBase64String(bytes);
|
|
|
|
+ System.out.println("base64编码:"+asB64+",length="+asB64.length()); //6L+Z5pivQmFzZTY057yW56CB5YmN55qE5Y6f5aeL5pWw5o2u77yB77yB77yB
|
|
|
|
+ System.out.println("从接口获取:nboVwAgESANAAqgAUACoAagBQABwArUGQl1pe0Vtu9g=,length="+"5ahSd9wDqAFoA0gDKALIA6gBcAKwBiWGZ45Ego3T084=".length());
|
|
// 解码
|
|
// 解码
|
|
byte[] asBytes = Base64.getDecoder().decode(asB64);
|
|
byte[] asBytes = Base64.getDecoder().decode(asB64);
|
|
|
|
+// byte[] asBytes =Base64.decodeBase64(asB64);
|
|
System.out.println("解码后数据="+new String(asBytes, code_format)); //这是Base64编码前的原始数据!!!
|
|
System.out.println("解码后数据="+new String(asBytes, code_format)); //这是Base64编码前的原始数据!!!
|
|
}
|
|
}
|
|
|
|
|
|
@@ -144,4 +154,92 @@ public class ByteTest {
|
|
System.out.println("binary: " + binary);
|
|
System.out.println("binary: " + binary);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testParseHexStr2Byte(){
|
|
|
|
+ String hexString = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
|
|
|
|
+ byte[] b2 = hexUtils.parseHexStr2Byte(hexString);
|
|
|
|
+ System.out.println(">>"+new String(b2));
|
|
|
|
+ System.out.println("b2.length="+b2.length);
|
|
|
|
+ for(int a=0;a<b2.length;a++){
|
|
|
|
+ System.out.println(""+b2[a]);
|
|
|
|
+ }
|
|
|
|
+// a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6
|
|
|
|
+ byte[] bytes2={65,66,67,68};
|
|
|
|
+ System.out.println(new String(bytes2));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void test1() throws UnsupportedEncodingException {
|
|
|
|
+ byte[] arrB = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6".getBytes("UTF-8");
|
|
|
|
+ int a=0;
|
|
|
|
+ for (byte b : arrB) {
|
|
|
|
+ a++;
|
|
|
|
+ System.out.println(a+" >>"+b);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+// String asB64 = Base64.getEncoder().encodeToString(arrB);
|
|
|
|
+// System.out.println("base64编码:"+asB64+",length="+asB64.length());
|
|
|
|
+ String b2 = hexUtils.byteArrToBinStr(arrB);
|
|
|
|
+ System.out.println(b2);
|
|
|
|
+ System.out.println("b2.length>>"+b2.length());
|
|
|
|
+ System.out.println("");
|
|
|
|
+ // 两个16进制字符表示一个字节长度
|
|
|
|
+ // 因为一个字节等于8bit(bit = 二进制位)
|
|
|
|
+ // 一个16进制字符 需要 4个二进制才能表示
|
|
|
|
+ // 所以两个16进制字符等于一个字节
|
|
|
|
+ //
|
|
|
|
+ int len = arrB.length;
|
|
|
|
+ byte[] arrOut = new byte[len / 2];
|
|
|
|
+ for (int i = 0; i < len; i += 2) {
|
|
|
|
+ // 上面得到的字节数组中,一个字符是一个长度,两个长度作为一个字符
|
|
|
|
+ String s = new String(arrB, i, 2, "UTF-8");
|
|
|
|
+ System.out.println((i+1)+","+s);
|
|
|
|
+ arrOut[i / 2] = (byte) Integer.parseInt(s, 16);
|
|
|
|
+ }
|
|
|
|
+// System.out.println((byte)Integer.parseInt("D0", 16));
|
|
|
|
+ // System.out.println((byte)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void test2a() throws UnsupportedEncodingException {
|
|
|
|
+// int[] hex=new int[32];
|
|
|
|
+// for(int a=0;a<32;a++){
|
|
|
|
+// hex[a]=0xe5;
|
|
|
|
+// }
|
|
|
|
+// System.out.println("hex.length="+hex.length);
|
|
|
|
+// for(int a=0;a<64;a++){
|
|
|
|
+// System.out.println((a+1)+" "+hex[a]);
|
|
|
|
+// byte[] bytes = String.valueOf(hex[a]).getBytes("UTF-8");
|
|
|
|
+// // 编码
|
|
|
|
+// String asB64 = Base64.getEncoder().encodeToString(bytes);
|
|
|
|
+// System.out.println("asB64="+asB64);
|
|
|
|
+// System.out.println("");
|
|
|
|
+// }
|
|
|
|
+//a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6
|
|
|
|
+// a5a0 894c 4c06 3803 6801 7800 6002 a001 b003 6802 2005 7505 921e 2234 41de 93c6
|
|
|
|
+ String hexStr = "e5";
|
|
|
|
+ String data = hexUtils.convertHexToString(hexStr);
|
|
|
|
+ System.out.println("data="+data);
|
|
|
|
+// String[] hex2=new String[32];
|
|
|
|
+ String d1 = "";
|
|
|
|
+ for(int a=0;a<16;a++){
|
|
|
|
+// hex2[a]="e5";
|
|
|
|
+ d1+=hexUtils.convertHexToString("a5");
|
|
|
|
+ }
|
|
|
|
+ System.out.println("d1="+d1+",length="+d1.length());
|
|
|
|
+ byte[] bytes = d1.getBytes("UTF-8");
|
|
|
|
+// String asB64 = Base64.getEncoder().encodeToString(bytes);
|
|
|
|
+// System.out.println("asB64="+asB64+",length="+asB64.length());
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void test5(){
|
|
|
|
+ String hello = "haQcTLQHKACAACADYAKoAegBUABoBwMy20ruynTJi94=";
|
|
|
|
+
|
|
|
|
+// byte[] decoded = Base64.decodeBase64(hello.getBytes());
|
|
|
|
+//
|
|
|
|
+// System.out.println(Arrays.toString(decoded));
|
|
|
|
+ }
|
|
}
|
|
}
|