This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git

commit ff68bb5371390edc9bb1509230ac50e427f6ce48
Author: Antoine Toulme <[email protected]>
AuthorDate: Mon Nov 25 21:28:01 2019 -0800

    Add ability to expose bytes as a hex string without a prefix
---
 bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java       | 12 ++++++++++++
 .../test/java/org/apache/tuweni/bytes/CommonBytesTests.java  | 11 +++++++++++
 2 files changed, 23 insertions(+)

diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java 
b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
index 36d00ce..ea7c347 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
@@ -1440,6 +1440,18 @@ public interface Bytes extends Comparable<Bytes> {
     }
   }
 
+  /**
+   * @return This value represented as hexadecimal, with no prefix.
+   */
+  default String toUnprefixedHexString() {
+    try {
+      return appendHexTo(new StringBuilder()).toString();
+    } catch (IOException e) {
+      // not thrown
+      throw new RuntimeException(e);
+    }
+  }
+
   /** @return This value represented as a minimal hexadecimal string (without 
any leading zero). */
   default String toShortHexString() {
     StringBuilder hex;
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/CommonBytesTests.java 
b/bytes/src/test/java/org/apache/tuweni/bytes/CommonBytesTests.java
index f3fcf28..c7aee0e 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/CommonBytesTests.java
+++ b/bytes/src/test/java/org/apache/tuweni/bytes/CommonBytesTests.java
@@ -434,6 +434,17 @@ abstract class CommonBytesTests {
   }
 
   @Test
+  void testHexString() {
+    assertEquals("0x", h("0x").toShortHexString());
+    assertEquals("0x", h("0x0000").toShortHexString());
+    assertEquals("0x1000001", h("0x01000001").toShortHexString());
+
+    assertEquals("0000", h("0x0000").toUnprefixedHexString());
+    assertEquals("1234", h("0x1234").toUnprefixedHexString());
+    assertEquals("0022", h("0x0022").toUnprefixedHexString());
+  }
+
+  @Test
   void slideToEnd() {
     assertEquals(Bytes.of(1, 2, 3, 4), Bytes.of(1, 2, 3, 4).slice(0));
     assertEquals(Bytes.of(2, 3, 4), Bytes.of(1, 2, 3, 4).slice(1));


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to