#
Junjie
1 天以前 536e17a446c170a8b214aaf188b98817ee6258c1
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.core.common;
 
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
 
public class RadixTools {
 
    public static void main(String[] args) {
        String val = toBinaryString((byte) 1);
        System.out.println(val);
        for (int i = val.length() - 1; i >= 0; i--) {
            char c = val.charAt(i);
            if (i == 7 && c == '1') {
                System.out.println("===");
            }
        }
    }
 
    public static String toBinaryString(byte b) {
        return Long.toBinaryString((b & 0xFF) + 0x100L).substring(1);
    }
 
    public static String toBinaryString(byte[] bytes) {
        StringBuilder builder = new StringBuilder();
        for (byte b : bytes) {
            builder.append(Long.toBinaryString((b & 0xFF) + 0x100L).substring(1));
        }
        return builder.toString();
    }
 
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || "".equals(hexString)) {
            return null;
        }
        String upper = hexString.toUpperCase();
        int length = upper.length() / 2;
        char[] chars = upper.toCharArray();
        byte[] result = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            result[i] = (byte) ((charToByte(chars[pos]) << 4) | charToByte(chars[pos + 1]));
        }
        return result;
    }
 
    public static String bytesToHexStr(byte[] bytes) {
        StringBuilder builder = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            builder.append(String.format("%02x", b & 0xFF));
        }
        return builder.toString();
    }
 
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
 
    public static String bytesToStr(byte[] bytes) {
        return bytesToStr(bytes, Charset.forName("gbk"));
    }
 
    public static String bytesToStr(byte[] bytes, Charset charset) {
        return new String(bytes, charset).trim().toUpperCase();
    }
 
    public static byte[] strToBytes(String str) throws UnsupportedEncodingException {
        return str.getBytes("gbk");
    }
 
    public static byte[] longToBytes(long value) {
        long tmp = value;
        byte[] bytes = new byte[8];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = Long.valueOf(tmp & 0xFF).byteValue();
            tmp = tmp >> 8;
        }
        return bytes;
    }
 
    public static long bytesToLong(byte[] bytes) {
        long result = 0L;
        long b0 = bytes[0] & 0xFFL;
        long b1 = (bytes[1] & 0xFFL) << 8;
        long b2 = (bytes[2] & 0xFFL) << 16;
        long b3 = (bytes[3] & 0xFFL) << 24;
        long b4 = (bytes[4] & 0xFFL) << 32;
        long b5 = (bytes[5] & 0xFFL) << 40;
        long b6 = (bytes[6] & 0xFFL) << 48;
        long b7 = (bytes[7] & 0xFFL) << 56;
        result = b0 | b1 | b2 | b3 | b4 | b5 | b6 | b7;
        return result;
    }
 
    public static byte[] intToBytes(int value) {
        int tmp = value;
        byte[] bytes = new byte[4];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = Integer.valueOf(tmp & 0xFF).byteValue();
            tmp = tmp >> 8;
        }
        return bytes;
    }
 
    public static int bytesToInt(byte[] bytes) {
        int result;
        int b0 = bytes[0] & 0xFF;
        int b1 = (bytes[1] & 0xFF) << 8;
        int b2 = (bytes[2] & 0xFF) << 16;
        int b3 = (bytes[3] & 0xFF) << 24;
        result = b0 | b1 | b2 | b3;
        return result;
    }
 
    public static short byteToShort(byte[] bytes) {
        short result;
        short b0 = (short) (bytes[0] & 0xFF);
        short b1 = (short) ((bytes[1] & 0xFF) << 8);
        result = (short) (b0 | b1);
        return result;
    }
 
    public static byte[] shortToByte(short value) {
        int tmp = value;
        byte[] bytes = new byte[2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = Integer.valueOf(tmp & 0xFF).byteValue();
            tmp = tmp >> 8;
        }
        return bytes;
    }
}