Skip to content

Commit

Permalink
Create ByteArrayWrapper from hex string
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraRoatis committed Feb 13, 2020
1 parent 2c3dcde commit 79ab01c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions modUtil/src/main/java/org/aion/util/types/ByteArrayWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.Objects;
import org.aion.util.conversions.Hex;

/**
* Immutable class used to wrap byte arrays for use in scenarios where value-based equality needs to
Expand Down Expand Up @@ -31,6 +32,23 @@ public static ByteArrayWrapper wrap(byte[] bytes) {
return new ByteArrayWrapper(bytes);
}

/**
* Returns a wrapper for the given hex string.
*
* @param hexString non-{@code null} string of hex characters to be wrapped
* @return a wrapper for the given hex string
* @throws NullPointerException if the given string is null
*/
public static ByteArrayWrapper fromHex(String hexString) {
Objects.requireNonNull(hexString, "The given string must not be null.");

String data = hexString.startsWith("0x") ? hexString.substring(2) : hexString;
if ((data.length() & 1) == 1) {
data = "0" + data;
}
return new ByteArrayWrapper(Hex.decode(data));
}

/**
* Returns a copy of the encapsulated byte array.
*
Expand Down

0 comments on commit 79ab01c

Please sign in to comment.