HELP! I don't know binary, hexadecimal, octal, and bit-wise

前端 未结 7 957
不知归路
不知归路 2021-02-10 23:25

I haven\'t learned this in programming class before, but now I need to know it. What are some good resources for learning these numbers and how to convert them? I pretty much am

相关标签:
7条回答
  • 2021-02-10 23:56

    Here my code in Python for Numeral conversions(2,8,10,16) from any to any. it may help you.

    class Conversion:
    
        def __init__(self):
            pass
    
        def dec_to_any(self, data, base):
            return base(data)
    
        def any_to_dec(self, data, base):
            return int(data, base)
    
    
    def main():
    
        menu ={1: 'dec to bin', 2:'dec to oct', 3:'dec to hex', 4: 'bin to dec', 5: 'bin to oct', 6:'bin to hex', 
               7: 'oct to bin', 8: 'oct to dec', 9: 'oct to hex', 10: 'hex to bin', 11: 'hex to oct', 12: 'hex to dec'}
    
        target_base = {'bin': bin,'oct': oct, 'hex': hex}    
        src_base = {'bin': 2,'oct': 8, 'hex': 16}
    
        choice=int(input(str(menu)+"\nEnter your choice: "))
    
        src, target = menu[choice].split()[0], menu[choice].split()[2]
    
        c=Conversion()
        val =input("Enter the value :")
    
        if(src == "dec"):
            val =int(val)
            value= c.dec_to_any(val, target_base[target])
            print('Value is :', value)
    
        elif(target == "dec"):
            value = c.any_to_dec(val, src_base[src])
            print('Value is :', value)
    
        else:
            val = c.any_to_dec(val, src_base[src])
            value= c.dec_to_any(val, target_base[target])
            print('Value is :', value)
    
    if __name__ == '__main__':
        main()
    
    0 讨论(0)
提交回复
热议问题