标签搜索

GDScript中二进制与十进制转换

admin
2021-02-04 / 0 评论 / 298 阅读 / 正在检测是否收录...

十进制数字转二进制字符串

# Takes in a decimal value (int) and returns the binary value (int)
func dec2bin(var decimal_value):
    var binary_string = "" 
    var temp 
    var count = 31 # Checking up to 32 bits 
    while(count >= 0):
        temp = decimal_value >> count 
        if(temp & 1):
            binary_string = binary_string + "1"
        else:
            binary_string = binary_string + "0"
        count -= 1
    return binary_string

二进制字符串转十进制数字

# Takes in a binary value (int) and returns the decimal value (int)
func bin2dec(var binary_value):
    var decimal_value = 0
    var count = 0
    var temp
    binary_value = int(binary_value)
    while(binary_value != 0):
        temp = binary_value % 10
        binary_value /= 10
        decimal_value += temp * pow(2, count)
        count += 1
    return decimal_value

参考/来源

0

评论 (0)

取消