Hex (Bouncy Castle Library 1.85 API Specification)
Date: 2026-07-12

org.bouncycastle.util.encoders

## Class Hex

- java.lang.Object
- - org.bouncycastle.util.encoders.Hex

-     public class Hex
      extends java.lang.Object

  Utility class for converting hex data to bytes and back again.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Hex()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static byte[]` | `decode(byte[] data)` decode the Hex encoded input data.
    `static byte[]` | `decode(byte[] data, int off, int length)` decode the Hex encoded input data.
    `static byte[]` | `decode(java.lang.String data)` decode the Hex encoded String data - whitespace will be ignored.
    `static int` | `decode(java.lang.String data, java.io.OutputStream out)` decode the Hex encoded String data writing it to the given output stream, whitespace characters will be ignored.
    `static byte[]` | `decodeStrict(java.lang.String str)` Decode the hexadecimal-encoded string strictly i.e. any non-hexadecimal characters will be considered an error.
    `static byte[]` | `decodeStrict(java.lang.String str, int off, int len)` Decode the hexadecimal-encoded string strictly i.e. any non-hexadecimal characters will be considered an error.
    `static byte[]` | `encode(byte[] data)` encode the input data producing a Hex encoded byte array.
    `static byte[]` | `encode(byte[] data, int off, int length)` encode the input data producing a Hex encoded byte array.
    `static int` | `encode(byte[] data, int off, int length, java.io.OutputStream out)` Hex encode the byte data writing it to the given output stream.
    `static int` | `encode(byte[] data, java.io.OutputStream out)` Hex encode the byte data writing it to the given output stream.
    `static java.lang.String` | `toHexString(byte[] data)`
    `static java.lang.String` | `toHexString(byte[] data, int off, int length)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Hex

          public Hex()

  - ### Method Detail

    - #### toHexString

          public static java.lang.String toHexString(byte[] data)

    - #### toHexString

          public static java.lang.String toHexString(byte[] data,
                                                     int off,
                                                     int length)

    - #### encode

          public static byte[] encode(byte[] data)

      encode the input data producing a Hex encoded byte array.
      Returns:
        a byte array containing the Hex encoded data.

    - #### encode

          public static byte[] encode(byte[] data,
                                      int off,
                                      int length)

      encode the input data producing a Hex encoded byte array.
      Returns:
        a byte array containing the Hex encoded data.

    - #### encode

          public static int encode(byte[] data,
                                   java.io.OutputStream out)
                            throws java.io.IOException

      Hex encode the byte data writing it to the given output stream.
      Returns:
        the number of bytes produced.
      Throws:
        `java.io.IOException`

    - #### encode

          public static int encode(byte[] data,
                                   int off,
                                   int length,
                                   java.io.OutputStream out)
                            throws java.io.IOException

      Hex encode the byte data writing it to the given output stream.
      Returns:
        the number of bytes produced.
      Throws:
        `java.io.IOException`

    - #### decode

          public static byte[] decode(byte[] data)

      decode the Hex encoded input data. It is assumed the input data is valid.
      Returns:
        a byte array representing the decoded data.

    - #### decode

          public static byte[] decode(byte[] data,
                                      int off,
                                      int length)

      decode the Hex encoded input data. It is assumed the input data is valid.
      Returns:
        a byte array representing the decoded data.

    - #### decode

          public static byte[] decode(java.lang.String data)

      decode the Hex encoded String data - whitespace will be ignored.
      Returns:
        a byte array representing the decoded data.

    - #### decode

          public static int decode(java.lang.String data,
                                   java.io.OutputStream out)
                            throws java.io.IOException

      decode the Hex encoded String data writing it to the given output stream, whitespace characters will be ignored.
      Returns:
        the number of bytes produced.
      Throws:
        `java.io.IOException`

    - #### decodeStrict

          public static byte[] decodeStrict(java.lang.String str)

      Decode the hexadecimal-encoded string strictly i.e. any non-hexadecimal characters will be considered an error.
      Returns:
        a byte array representing the decoded data.

    - #### decodeStrict

          public static byte[] decodeStrict(java.lang.String str,
                                            int off,
                                            int len)

      Decode the hexadecimal-encoded string strictly i.e. any non-hexadecimal characters will be considered an error.
      Returns:
        a byte array representing the decoded data.