Class HexStringHelper
Provides utility methods for normalizing and converting variable-length hex-encoded string fields used throughout the XRP Ledger protocol. Fields such as CredentialType, URI, and DIDDocument are stored on-chain as hex-encoded binary blobs. This helper converts between human-readable text and the canonical hex representation.
Inheritance
Namespace: Xrpl.Models.Utils
Assembly: Xrpl.dll
Syntax
public static class HexStringHelper
Methods
| Edit this page View SourceFromHex(string)
Decodes a hex-encoded string back to its human-readable UTF-8 representation. Trailing null bytes (0x00) are trimmed.
Declaration
public static string FromHex(string hex)
Parameters
| Type | Name | Description |
|---|---|---|
| string | hex | Hex-encoded string. |
Returns
| Type | Description |
|---|---|
| string | Decoded UTF-8 string, or null if input is null/empty. |
IsValidHex(string)
Checks if a string contains only valid hexadecimal characters and has an even length (complete byte pairs).
Declaration
public static bool IsValidHex(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value |
Returns
| Type | Description |
|---|---|
| bool |
NormalizeToHex(string, int, string, int)
Normalizes a value for a hex-encoded VL field. If the value is already valid hex, it is uppercased and returned as-is. Otherwise, the value is treated as UTF-8 text and encoded to hex.
Declaration
public static string NormalizeToHex(string value, int maxBytes = 0, string fieldName = null, int padToBytes = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | Raw value (hex string or plain text). |
| int | maxBytes | Maximum allowed byte length (e.g. 64 for CredentialType). |
| string | fieldName | Field name for error messages. |
| int | padToBytes | If greater than zero, the result is right-padded with zero bytes to exactly this size. Use for fixed-length fields such as UInt256 / Hash256 (32 bytes). Defaults to 0 (no padding). |
Returns
| Type | Description |
|---|---|
| string | Uppercased hex string, or null if input is null/empty. |