‘Java Hex To Byte & CRCChecker’

  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

public final String byte2Hex(final byte[] btSrc) throws Exception {
   
final StringBuffer sbuffer = new StringBuffer();
   
if (btSrc != null) {
   
for (int n = 0; n < btSrc.length; n++) {
   
String stmp = "00" + Integer.toHexString(btSrc[n] & 0XFF);
   
stmp = stmp.substring(stmp.length() - 2);
   
sbuffer.append(stmp.toUpperCase());
   
}
   
}
   
return sbuffer.toString();
   
}

/**
   
* Hex2 byte.
   
*
   
* @param src
   
* the src
   
* @return the byte[]
   
*/
   
public final byte[] hex2Byte(final String src) throws Exception {
   
byte[] btRtn = new byte[0];
   
String srcTmp = src;
   
if (isHex(srcTmp)) {
   
int len = srcTmp.length();
   
if (len % 2 != 0) {
   
len++;
   
srcTmp = "0" + srcTmp;
   
}
   
len = len / 2;
   
btRtn = new byte[len];
   
for (int n = 0; n < len; n++) {
   
final int index = 2 * n;
   
final String strTemp = String.valueOf(srcTmp.charAt(index))
   
+ String.valueOf(srcTmp.charAt(index + 1));
   
final byte btTemp = (byte) Integer.parseInt(strTemp, 16);
   
btRtn[n] = btTemp;
   
}
   
}
   
return btRtn;
   
}

/**
   
* Checks if is hex.
   
*
   
* @param src
   
* the src
   
* @return true, if is hex
   
*/
   
public final boolean isHex(final String src) {
   
boolean blRtn = false;
   
if (src != null) {
   
final String pattern = "([0-9a-fA-F])*";
   
blRtn = src.matches(pattern);
   
}
   
return blRtn;
   
}

CRCChecker.java

 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

import java.io.File;
  
import java.io.FileInputStream;
  
import java.io.IOException;
  
import java.io.InputStream;
  
import java.util.zip.CRC32;
  
import java.util.zip.CheckedInputStream;

public class CRCChecker {

public static long getCRC(File file) throws Exception{
   
long lngRtn = 0;
   
if (file != null && file.isFile() && file.exists()) {
   
InputStream input = null;
   
CheckedInputStream checkedInput = null;
   
try {
   
input = MagicBoxHelper.validate(new FileInputStream(file));
   
CRC32 crc32 = new CRC32();
   
checkedInput = new CheckedInputStream(
   
input, crc32);
   
int intRtn = checkedInput.read();
   
while (intRtn != -1) {
   
intRtn = checkedInput.read();
   
}
   
lngRtn = crc32.getValue();
   
} catch (Exception e) {
   
throw e;
   
} finally {
   
if (checkedInput != null) {
   
try {
   
checkedInput.close();
   
} catch (IOException e) {
   
throw e;
   
}
   
}
   
if (input != null) {
   
try {
   
input.close();
   
} catch (IOException e) {
   
throw e;
   
}
   
}
   
}
   
}
   
return lngRtn;
   
}

}