forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUInt160.java
More file actions
58 lines (52 loc) · 1.39 KB
/
Copy pathUInt160.java
File metadata and controls
58 lines (52 loc) · 1.39 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
package DNA;
/**
* Custom type which inherits base class defines 20-bit data,
* it mostly used to defined contract address
*
* @author 12146
* @since JDK1.8
*
*/
public class UInt160 extends UIntBase implements Comparable<UInt160> {
public static final UInt160 ZERO = new UInt160();
public UInt160() {
this(null);
}
public UInt160(byte[] value) {
super(20, value);
}
@Override
public int compareTo(UInt160 other) {
byte[] x = this.data_bytes;
byte[] y = other.data_bytes;
for (int i = x.length - 1; i >= 0; i--) {
int r = Byte.toUnsignedInt(x[i]) - Byte.toUnsignedInt(y[i]);
if (r != 0) {
return r;
}
}
return 0;
}
public static UInt160 parse(String value) {
if (value == null) {
throw new NullPointerException();
}
if (value.startsWith("0x")) {
value = value.substring(2);
}
if (value.length() != 40) {
throw new IllegalArgumentException();
}
byte[] v = Helper.hexToBytes(value);
return new UInt160(Helper.reverse(v));
}
public static boolean tryParse(String s, UInt160 result) {
try {
UInt160 v = parse(s);
result.data_bytes = v.data_bytes;
return true;
} catch (Exception e) {
return false;
}
}
}