Thanks to visit codestin.com
Credit goes to ssojet.com

Base36 in Groovy

December 3, 2025 3 min read

Working with Base36 encoded numbers, especially in Groovy, can be cumbersome if you're not familiar with the process. This guide breaks down how to effectively encode and decode Base36 strings within your Groovy applications. You'll learn the core Groovy methods involved and see practical examples that demonstrate real-world usage, ultimately simplifying your data manipulation tasks.

Encoding to Base36

Converting integers to their Base36 string representation is a practical way to generate shorter, more human-readable identifiers compared to standard Base10 or Base16. This encoding uses digits 0-9 and letters A-Z.

Groovy makes this straightforward using the toString(radix) method available on numeric types. For instance, to encode a number to Base36, you simply call toString(36) on the integer or long.

def number = 123456789L
def base36Encoded = number.toString(36)
// base36Encoded will be "2n9c"

A common pitfall is using Integer for large numbers. Integer has a limited range, and attempting to encode values exceeding its maximum will lead to incorrect results due to overflow. Always opt for Long when dealing with potentially large numbers to ensure accurate Base36 conversion.

When generating unique IDs or short aliases, remember to leverage Long for dependable Base36 encoding.

Decoding from Base36

To convert a Base36 encoded string back into its original numerical representation, you'll use the parseLong method, specifying 36 as the radix. This is the direct inverse of the encoding process.

For instance, if you have a Base36 string like "2n9c", you can decode it to a long value using:

def base36String = "2n9c"
def originalValue = Long.parseLong(base36String, 36)
println originalValue // Output: 123456789

A common pitfall to watch out for is providing a string that includes characters not present in the Base36 alphabet (0-9 and a-z). In such cases, Groovy will throw a NumberFormatException. Always validate your input strings before attempting to decode them to prevent runtime errors.

Generating Unique IDs

You can leverage Base36 encoding to create concise, unique identifiers by combining it with other data points. A robust pattern involves concatenating a millisecond timestamp with a random suffix before encoding. This approach ensures both uniqueness and a degree of unpredictability.

For instance, you could generate an ID like this:

def timestamp = System.currentTimeMillis()
def randomSuffix = new Random().nextInt(1000) // Generates a random number between 0 and 999
def combinedValue = timestamp * 1000 + randomSuffix // Scale timestamp to make space for suffix
def uniqueId = combinedValue.toString(36)

println uniqueId

A common pitfall is relying solely on a timestamp, especially in high-concurrency environments. Without a random component, identical timestamps could lead to ID collisions. By incorporating a random number, you significantly reduce the probability of generating duplicate IDs. This method provides a practical way to generate short, unique strings suitable for various applications.

Handling Large Numbers and Performance

Groovy's automatic type conversion is convenient, but it's worth considering performance implications when dealing with very large numbers or frequent Base36 operations. For values exceeding the capacity of standard long types, Groovy's BigInteger is your go-to. You can easily convert a BigInteger to Base36 using toString(36) or construct one from a Base36 string with new BigInteger(base36String, 36).

For instance, decoding a long Base36 identifier might look like this:

def base36Id = "1a2b3c4d5e6f7g8h9i0j"
def bigNumber = new BigInteger(base36Id, 36)
println bigNumber

A common gotcha is that frequent BigInteger conversions to and from Base36 can be more computationally intensive than operations on primitive long types. If you're pushing performance limits, profile your conversion logic to ensure it isn't a bottleneck. Prioritize primitive types for speed where possible, reserving BigInteger for truly massive values.

Related Articles

Base64 in Swift

Encode and decode data using Base64 in Swift. Learn practical implementation for file handling and API communication.

December 30, 2025 3 min read
Read full article

z85 (ZeroMQ spec:32/Z85) in PHP

Encode/decode binary data with Z85 in PHP. Implement Z85 encoding for secure, compact data transfer directly in your PHP applications.

December 30, 2025 4 min read
Read full article

Base58 in NodeJS

Encode and decode Base58 efficiently in NodeJS. Implement Bitcoin addresses, IPFS CIDs, and more with this essential developer tool.

December 30, 2025 3 min read
Read full article

Intel HEX in TypeScript

Convert Intel HEX files to TypeScript with this practical tool. Streamline your embedded development workflow.

December 29, 2025 4 min read
Read full article