package v1 import ( "strconv" "strings" ufmt "gno.land/p/nt/ufmt/v0" ) // EncodeUint converts a uint64 number into a zero-padded 20-character string. // // Parameters: // - num (uint64): The number to encode. // // Returns: // - string: A zero-padded string representation of the number. // // Example: // Input: 12345 // Output: "00000000000000012345" func EncodeUint(num uint64) string { // Convert the value to a decimal string. s := strconv.FormatUint(num, 10) // Zero-pad to a total length of 20 characters. zerosNeeded := 20 - len(s) return strings.Repeat("0", zerosNeeded) + s } func EncodeInt64(num int64) string { if num < 0 { panic(ufmt.Sprintf("negative value not supported: %d", num)) } return EncodeUint(uint64(num)) } // DecodeUint converts a zero-padded string back into a uint64 number. // // Parameters: // - s (string): The zero-padded string. // // Returns: // - uint64: The decoded number. // // Panics: // - If the string cannot be parsed into a uint64. // // Example: // Input: "00000000000000012345" // Output: 12345 func DecodeUint(s string) uint64 { num, err := strconv.ParseUint(s, 10, 64) if err != nil { panic(err) } return num } func DecodeInt64(s string) int64 { num, err := strconv.ParseInt(s, 10, 64) if err != nil { panic(err) } return num }