2021与蓝度共同重构项目,服务端
liuhaonan
2022-11-07 ddfbc40f9ca8546a2c34865abef51630f054d5e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.sandu.ximon.admin.utils;
 
import com.sandu.ximon.admin.manager.iot.rrpc.util.SupplementUtils;
import org.apache.commons.codec.binary.Base64;
 
public class Base64Util {
 
    /**
     * 将二进制数据编码为BASE64字符串
     * @param binaryData
     * @return
     */
    public static String encode(byte[] binaryData) {
        try {
            return new String(Base64.encodeBase64(binaryData));
        } catch (Exception e) {
            return null;
        }
    }
 
    /**
     * 将BASE64字符串恢复为二进制数据
     * @param base64String
     * @return
     */
    public static byte[] decode(String base64String) {
        try {
            return Base64.decodeBase64(base64String.getBytes());
        } catch (Exception e) {
            return null;
        }
    }
 
//    public static void main(String[] args) {
//        byte[] bytes = "/qUBAAv+AQAD//8yjUBF9xgeI0U=".getBytes();
//        byte[] bytes1 = Base64.decodeBase64(bytes);
//        System.out.println(SupplementUtils.bytesToHexString(bytes1));
//    }
 
 
    public static String toBase64Frame (String hexStr) {
//        BigInteger bigInteger = new BigInteger(hexStr, 16);   //  此方式会产生头部多出空的一字节
//        byte[] bytes = bigInteger.toByteArray();
//        System.out.println("origin:"+hexStr);
        byte[] bytes = SupplementUtils.hexStringToBytes(hexStr);
        String base64 = encode(bytes);
        return base64;
    }
}