16进制字符串转2进制
2进制转16进制字符串
查找byte数组中存在的重复连续的位置第一个
复制代码
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
156public class Main1 { public static void main(String[] args) { System.out.println("Hello world! "); } /** * 查找byte数组中存在的重复连续的位置第一个 * * @param source {0x01,0x02,0x03} * @param dest {0x01,0x02} * @return 0 */ public static int indexOf(byte[] source, byte[] dest) { if (null == source || source.length <= 0 || null == dest || dest.length <= 0) { return -1; } int sourceLen = source.length; int destLen = dest.length; if (sourceLen < destLen) { return -1; } for (int i = 0; i <= sourceLen - destLen; i++) { int index = i; for (int j = 0; j < destLen; j++) { byte bt = source[j + i]; if (bt != dest[j]) { index = -1; break; } } if (index != -1) { return index; } } return -1; } /** * 求和 * * @param buff * @return */ public static int sumByte(byte[] buff) { int sum = 0; if (null == buff || buff.length <= 0) { return sum; } for (byte b : buff) { sum += byte2int(b); } return sum & 0xFF; } /** * byte转int * * @param b * @return */ public static int byte2int(byte b) { return b < 0 ? 256 + b : b; } public static String byte2hexStr(byte b) { String s = Integer.toHexString(0xFF & b); if (s.length() < 2) { s = "0" + s; } return s.toUpperCase(); } /** * 2进制转16进制字符串 * * @param b * @return */ public static String byte2hexStr(byte[] b) { if (null == b || b.length <= 0) { return ""; } StringBuilder sb = new StringBuilder(b.length); for (byte value : b) { sb.append(byte2hexStr(value)); } return sb.toString(); } /** * 字符串转byte数组 * * @param hex * @return */ public static byte[] hexStr2byte(String hex) { byte[] b = null; if (null == hex || hex.length() <= 0) { return b; } hex = hex.toUpperCase(); int len = hex.length() / 2; char[] hexChars = hex.toCharArray(); b = new byte[len]; for (int i = 0; i < len; i++) { int pos = i * 2; b[i] = (byte) (char2byte(hexChars[pos]) << 4 | char2byte(hexChars[pos + 1])); } return b; } /** * char 转byte * * @param c * @return */ public static byte char2byte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } /** * 两个字节表示数据长度 * * @param len 255 * @return 00FF */ public static byte[] setLen(int len) { byte[] buff = new byte[2]; buff[0] = (byte) (len / 256); buff[1] = (byte) (len % 256); return buff; } /** * 解析两位的数据长度 00FF * * @param l 00 * @param r FF * @return 255 */ public static int getLen(byte l, byte r) { return byte2int(l) * 256 + byte2int(r); } }
最后
以上就是英勇板栗最近收集整理的关于java byte 16进制字符串相关操作的全部内容,更多相关java内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复