HEX.java
3.98 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.huaheng.mes.nfc;
/**
*
* @author houj
*
*/
public final class HEX {
/**
* HEX
*
* @param s
* @return
*/
public static byte[] hexToBytes(String s) {
s = s.toUpperCase();
int len = s.length() / 2;
int ii = 0;
byte[] bs = new byte[len];
char c;
int h;
for (int i = 0; i < len; i++) {
c = s.charAt(ii++);
if (c <= '9') {
h = c - '0';
} else {
h = c - 'A' + 10;
}
h <<= 4;
c = s.charAt(ii++);
if (c <= '9') {
h |= c - '0';
} else {
h |= c - 'A' + 10;
}
bs[i] = (byte) h;
}
return bs;
}
private final static char[] CS = "0123456789ABCDEF".toCharArray();
/**
* @param bs
* @return
*/
public static String bytesToHex(byte[] bs) {
char[] cs = new char[bs.length * 2];
int io = 0;
for (int n : bs) {
cs[io++] = CS[(n >> 4) & 0xF];
cs[io++] = CS[(n >> 0) & 0xF];
}
return new String(cs);
}
public static String bytesToHex(byte[] bs, int len) {
char[] cs = new char[len * 2];
int io = 0;
for (int i = 0; i < len; i++) {
int n = bs[i];
cs[io++] = CS[(n >> 4) & 0xF];
cs[io++] = CS[(n >> 0) & 0xF];
}
return new String(cs);
}
public static String bytesToHex(byte[] bs, char gap) {
char[] cs = new char[bs.length * 3];
int io = 0;
for (int n : bs) {
cs[io++] = CS[(n >> 4) & 0xF];
cs[io++] = CS[(n >> 0) & 0xF];
cs[io++] = gap;
}
return new String(cs);
}
public static String bytesToHex(byte[] bs, char gap, int len) {
char[] cs = new char[len * 3];
int io = 0;
for (int i = 0; i < len; i++) {
int n = bs[i];
cs[io++] = CS[(n >> 4) & 0xF];
cs[io++] = CS[(n >> 0) & 0xF];
cs[io++] = gap;
}
return new String(cs);
}
/**
*
*
* @param bs
* @param bytePerLine
* @return
*/
public static String bytesToCppHex(byte[] bs, int bytePerLine) {
if (bytePerLine <= 0 || bytePerLine >= 65536) {
bytePerLine = 65536;
}
int lines = 0;
if (bytePerLine < 65536) {
lines = (bs.length + bytePerLine - 1) / bytePerLine;
}
char[] cs = new char[bs.length * 5 + lines * 3];
int io = 0;
int ic = 0;
for (int n : bs) {
cs[io++] = '0';
cs[io++] = 'x';
cs[io++] = CS[(n >> 4) & 0xF];
cs[io++] = CS[(n >> 0) & 0xF];
cs[io++] = ',';
if (bytePerLine < 65536) {
if (++ic >= bytePerLine) {
ic = 0;
cs[io++] = '/';
cs[io++] = '/';
cs[io++] = '\n';
}
}
}
if (bytePerLine < 65536) {
if (io < cs.length) {
cs[io++] = '/';
cs[io++] = '/';
cs[io++] = '\n';
}
}
return new String(cs);
}
public static String toLeHex(int n, int byteCount) {
char[] rs = new char[byteCount * 2];
int io = 0;
for (int i = 0; i < byteCount; i++) {
rs[io++] = CS[(n >> 4) & 0xF];
rs[io++] = CS[(n >> 0) & 0xF];
n >>>= 8;
}
return new String(rs);
}
public static String toBeHex(int n, int byteCount) {
char[] rs = new char[byteCount * 2];
int io = 0;
n <<= (32 - byteCount * 8);
for (int i = 0; i < byteCount; i++) {
rs[io++] = CS[(n >> 28) & 0xF];
rs[io++] = CS[(n >> 24) & 0xF];
n <<= 8;
}
return new String(rs);
}
// public static void main(String[] args) {
// byte[] bs =
// HEX.hexToBytes("00C6B0F084C0B05BBEC211A678114CA6C751C08FE99EE22D25F7DCC21440D4ECA193C3444D8A8C53DDE6FBD40AEB917B551119D61A6A347CEF64CAB7A9437D2D9B34478DCB256CDCDDBB8E2A4E2D5F631136C7AA91037898D65B83526D5BE1978C818B9DD60CD19D5007269B966407D7D05A9BAFFB0964BE4DDEC331D697D07C1B");
// System.out.println(bs.length);
//
// String s =
// "80 94 db 61 fa 66 16 d0 c9 d4 3d 3b 81 b4 72 a0 98 35 40 9b 71 4a ef fc 16 15 32 bd 7f a7 ba 7f 12 e2 e6 21 03 87 b1 9f c5 bc 12 dd 63 5c 27 1b 11 73 e9 08 26 85 29 96 c6 57 e4 e5 e1 db f2 e5 40 ff c0 fa 6c 27 90 f2 23 c0 20 02 3f c0 ae de 30 e7 89 71 33 10 a7 eb 7b c6 41 1b 13 b4 0f 22 25 cc d4 a1 ba 1b 0c a0 8b 7b 5c cd be ed 36 d4 5e 64 63 66 62 18 26 89 d4 c1 8c 09 56 c3 33 f8 03";
// s = s.replace(" ", "");
// bs = HEX.hexToBytes(s);
// System.out.println(bs.length);
// }
public static void main(String[] args) {
System.out.println(toBeHex(0x3f01, 2));
}
}