25.04.2025

Алгоритмы перевода в разные системы счисления

# В 10-ю систему
# 1235(7)=x(10)
a = (1 * 7 ** 3) + (2 * 7 ** 2) + (3 * 7 ** 1) + (5 * 7 ** 0)
print(a)

# Из 10-й в любую другую
# 1234(10) = x(6)
n=1234
s=""
while (n>0):
    ost=n%6
    n=n//6
    s+=str(ost)
s=s[::-1] # переворот строки
print(s)

# Из 10-й в 16-ю
# 1234(10) = x(16)
n=1234
s=""
while (n>0):
    ost=n%16
    n=n//16
    if ost==10:
        s+='a'
    if ost==11:
        s+='b'
    if ost==12:
        s+='c'
    if ost==13:
        s+='d'
    if ost==14:
        s+='e'
    if ost==15:
        s+='f'
    if ost<=9:
        s+=str(ost)
s=s[::-1] # переворот строки
print(s)