Mirror function of num_uint64

Godot Version

4.3

Question

I’ve been digging around for some time; I’ve found that the num_uinr64() function allows the conversion of an integer to any base like binary, hex or anything in between and beyond.
However I cannot find the function that would take a number of a specified base to turn it back into an int.
Is there such a function ? Or do I have to make it myself ? Thank you so much in advance !

If you need to convert from an unusual base (beyond standard bases like 2, 8, 16), you would need to implement your own conversion function. Something like this could work…

func custom_base_to_int(number: String, base: int) -> int:
    var result = 0
    var power = 0
    
    # Convert from right to left
    for digit in number.to_lower().reverse():
        var value
        # Handle alphanumeric digits
        if digit.is_valid_int():
            value = digit.to_int()
        else:
            # Convert a-z to 10-35
            value = ord(digit) - ord("a") + 10
            
        if value >= base:
            push_error("Invalid digit for given base")
            return 0
            
        result += value * pow(base, power)
        power += 1
        
    return result
2 Likes

The two most common ones are already there in the string class:

bin_to_int()  
hex_to_int()

Thank you ! I was wondering if there was any function as I’ve seen there seemed to be one in godot 3.
Anyways, thank you for the script but I will make my own :stuck_out_tongue:
have an amazing day !

1 Like