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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.util;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
public class HexDump
{
private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private final static char[] HEX_LOWER_CASE_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
public static String dumpHexString(@Nullable byte[] array) {
if (array == null) return "(null)";
return dumpHexString(array, 0, array.length);
}
public static String dumpHexString(@Nullable byte[] array, int offset, int length)
{
if (array == null) return "(null)";
StringBuilder result = new StringBuilder();
byte[] line = new byte[16];
int lineIndex = 0;
result.append("\n0x");
result.append(toHexString(offset));
for (int i = offset ; i < offset + length ; i++)
{
if (lineIndex == 16)
{
result.append(" ");
for (int j = 0 ; j < 16 ; j++)
{
if (line[j] > ' ' && line[j] < '~')
{
result.append(new String(line, j, 1));
}
else
{
result.append(".");
}
}
result.append("\n0x");
result.append(toHexString(i));
lineIndex = 0;
}
byte b = array[i];
result.append(" ");
result.append(HEX_DIGITS[(b >>> 4) & 0x0F]);
result.append(HEX_DIGITS[b & 0x0F]);
line[lineIndex++] = b;
}
if (lineIndex != 16)
{
int count = (16 - lineIndex) * 3;
count++;
for (int i = 0 ; i < count ; i++)
{
result.append(" ");
}
for (int i = 0 ; i < lineIndex ; i++)
{
if (line[i] > ' ' && line[i] < '~')
{
result.append(new String(line, i, 1));
}
else
{
result.append(".");
}
}
}
return result.toString();
}
public static String toHexString(byte b)
{
return toHexString(toByteArray(b));
}
@UnsupportedAppUsage
public static String toHexString(byte[] array)
{
return toHexString(array, 0, array.length, true);
}
@UnsupportedAppUsage(maxTargetSdk = 30, trackingBug = 170729553)
public static String toHexString(byte[] array, boolean upperCase)
{
return toHexString(array, 0, array.length, upperCase);
}
@UnsupportedAppUsage
public static String toHexString(byte[] array, int offset, int length)
{
return toHexString(array, offset, length, true);
}
public static String toHexString(byte[] array, int offset, int length, boolean upperCase)
{
char[] digits = upperCase ? HEX_DIGITS : HEX_LOWER_CASE_DIGITS;
char[] buf = new char[length * 2];
int bufIndex = 0;
for (int i = offset ; i < offset + length; i++)
{
byte b = array[i];
buf[bufIndex++] = digits[(b >>> 4) & 0x0F];
buf[bufIndex++] = digits[b & 0x0F];
}
return new String(buf);
}
@UnsupportedAppUsage
public static String toHexString(int i)
{
return toHexString(toByteArray(i));
}
public static byte[] toByteArray(byte b)
{
byte[] array = new byte[1];
array[0] = b;
return array;
}
public static byte[] toByteArray(int i)
{
byte[] array = new byte[4];
array[3] = (byte)(i & 0xFF);
array[2] = (byte)((i >> 8) & 0xFF);
array[1] = (byte)((i >> 16) & 0xFF);
array[0] = (byte)((i >> 24) & 0xFF);
return array;
}
private static int toByte(char c)
{
if (c >= '0' && c <= '9') return (c - '0');
if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
throw new RuntimeException ("Invalid hex char '" + c + "'");
}
@UnsupportedAppUsage
public static byte[] hexStringToByteArray(String hexString)
{
int length = hexString.length();
byte[] buffer = new byte[length / 2];
for (int i = 0 ; i < length ; i += 2)
{
buffer[i / 2] = (byte)((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i+1)));
}
return buffer;
}
public static StringBuilder appendByteAsHex(StringBuilder sb, byte b, boolean upperCase) {
char[] digits = upperCase ? HEX_DIGITS : HEX_LOWER_CASE_DIGITS;
sb.append(digits[(b >> 4) & 0xf]);
sb.append(digits[b & 0xf]);
return sb;
}
}
|