Class OpenHex

java.lang.Object
cloud.opencode.base.core.OpenHex

public final class OpenHex extends Object
Hexadecimal Utility Class - Hex encoding, decoding and validation 十六进制工具类 - 十六进制编码、解码和验证

Provides hexadecimal encoding and decoding operations.

提供十六进制编解码功能。

Features | 主要功能:

  • Encoding (encodeHex, encodeHexUpper, byteToHex) - 编码
  • Decoding (decodeHex from String/char[]) - 解码
  • Validation (isHexNumber, isHexString) - 验证
  • Formatting (format with spaces, normalize) - 格式化
  • Integer conversion (toHex, toInt, toLong) - 整数转换

Usage Examples | 使用示例:

// Encoding - 编码
String hex = OpenHex.encodeHex(bytes);           // "48656c6c6f"
String upper = OpenHex.encodeHexUpper(bytes);    // "48656C6C6F"

// Decoding - 解码
byte[] data = OpenHex.decodeHex("48656c6c6f");

// Validation - 验证
boolean valid = OpenHex.isHexString("0f1a2b");   // true

// Formatting - 格式化
String formatted = OpenHex.format("48656c6c6f"); // "48 65 6c 6c 6f"

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    byteToHex(byte b)
    Converts a single byte to a hex string 单字节转十六进制字符串
    static byte[]
    decodeHex(char[] hex)
    Converts hex char array to byte array 十六进制字符数组转字节数组
    static byte[]
    Converts hex string to byte array 十六进制字符串转字节数组
    static String
    encodeHex(byte[] data)
    Converts byte array to lowercase hex string 字节数组转十六进制字符串(小写)
    static char[]
    encodeHexChars(byte[] data)
    Converts byte array to hex char array 字节数组转十六进制字符数组
    static char[]
    encodeHexChars(byte[] data, boolean toLowerCase)
    Converts byte array to hex char array 字节数组转十六进制字符数组
    static String
    encodeHexUpper(byte[] data)
    Converts byte array to uppercase hex string 字节数组转十六进制字符串(大写)
    static String
    Formats hex string with spaces between each byte pair 格式化十六进制字符串(每两位加空格)
    static boolean
    Checks whether the string is a hex number 判断是否为十六进制数
    static boolean
    Checks whether the string is a valid hex string 判断是否为有效的十六进制字符串
    static String
    Removes spaces and separators from a hex string 移除十六进制字符串中的空格和分隔符
    static String
    toHex(int value)
    Converts an integer to a hex string 整数转十六进制字符串
    static String
    toHex(long value)
    Converts a long to a hex string 长整数转十六进制字符串
    static int
    Converts a hex string to an integer 十六进制字符串转整数
    static long
    Converts a hex string to a long 十六进制字符串转长整数

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • encodeHex

      public static String encodeHex(byte[] data)
      Converts byte array to lowercase hex string 字节数组转十六进制字符串(小写)
    • encodeHexUpper

      public static String encodeHexUpper(byte[] data)
      Converts byte array to uppercase hex string 字节数组转十六进制字符串(大写)
    • encodeHexChars

      public static char[] encodeHexChars(byte[] data)
      Converts byte array to hex char array 字节数组转十六进制字符数组
    • encodeHexChars

      public static char[] encodeHexChars(byte[] data, boolean toLowerCase)
      Converts byte array to hex char array 字节数组转十六进制字符数组
    • byteToHex

      public static String byteToHex(byte b)
      Converts a single byte to a hex string 单字节转十六进制字符串
    • decodeHex

      public static byte[] decodeHex(String hex)
      Converts hex string to byte array 十六进制字符串转字节数组
    • decodeHex

      public static byte[] decodeHex(char[] hex)
      Converts hex char array to byte array 十六进制字符数组转字节数组
    • isHexNumber

      public static boolean isHexNumber(String str)
      Checks whether the string is a hex number 判断是否为十六进制数
    • isHexString

      public static boolean isHexString(String str)
      Checks whether the string is a valid hex string 判断是否为有效的十六进制字符串
    • format

      public static String format(String hex)
      Formats hex string with spaces between each byte pair 格式化十六进制字符串(每两位加空格)
    • normalize

      public static String normalize(String hex)
      Removes spaces and separators from a hex string 移除十六进制字符串中的空格和分隔符
    • toHex

      public static String toHex(int value)
      Converts an integer to a hex string 整数转十六进制字符串
    • toHex

      public static String toHex(long value)
      Converts a long to a hex string 长整数转十六进制字符串
    • toInt

      public static int toInt(String hex)
      Converts a hex string to an integer 十六进制字符串转整数
    • toLong

      public static long toLong(String hex)
      Converts a hex string to a long 十六进制字符串转长整数