forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUInt256.java
More file actions
52 lines (45 loc) · 1.31 KB
/
Copy pathUInt256.java
File metadata and controls
52 lines (45 loc) · 1.31 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
package DNA;
public class UInt256 extends UIntBase implements Comparable<UInt256> {
public static final UInt256 ZERO = new UInt256();
public UInt256() {
this(null);
}
public UInt256(byte[] value) {
super(32, value);
}
@Override
public int compareTo(UInt256 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 UInt256 parse(String s) {
if (s == null) {
throw new NullPointerException();
}
if (s.startsWith("0x")) {
s = s.substring(2);
}
if (s.length() != 64) {
throw new IllegalArgumentException(String.format("字符串\"{0}\"无法识别为正确的UInt256。", s));
}
byte[] v = Helper.hexToBytes(s);
// return new UInt256(Helper.reverse(v));
return new UInt256(v);
}
public static boolean tryParse(String s, UInt256 result) {
try {
UInt256 v = parse(s);
result.data_bytes = v.data_bytes;
return true;
} catch (Exception e) {
return false;
}
}
}