barcode

n진수->10진수 코딩하기

Coding/Python

https://koreanraichu.tistory.com/254

 

백준 2745번 풀이

문제 https://www.acmicpc.net/problem/2745 2745번: 진법 변환 B진법 수 N이 주어진다. 이 수를 10진법으로 바꿔 출력하는 프로그램을 작성하시오. 10진법을 넘어가는 진법은 숫자로 표시할 수 없는 자리가 있

koreanraichu.tistory.com

여기서 잠깐 언급했던 그거 맞다. 그것까지 주저리주저리 넣었다간 대참사 터져서 따로 뺐다. 참고로 기본 원리 역시 저기 서술되어있으므로 여기서는 생략.

 

a = input()
cipher = len(a) # 자릿수가 영어로 cipher였다니
a = a[::-1]
dec_number = 0

for i in range(cipher):
    dec_number += int(a[i]) * (2 ** i)

print(dec_number)

https://github.com/koreanraichu/Python/blob/master/Bin%20to%20Dec.py

 

GitHub - koreanraichu/Python: Upload my file

Upload my file . Contribute to koreanraichu/Python development by creating an account on GitHub.

github.com

 

a = input()
cipher = len(a) # 자릿수가 영어로 cipher였다니
a = a[::-1]
dec_number = 0

for i in range(cipher):
    dec_number += int(a[i]) * (8 ** i)

print(dec_number)

https://github.com/koreanraichu/Python/blob/master/Oct%20to%20Dec.py

 

GitHub - koreanraichu/Python: Upload my file

Upload my file . Contribute to koreanraichu/Python development by creating an account on GitHub.

github.com

 

a = input()
cipher = len(a) # 자릿수가 영어로 cipher였다니
a = a[::-1]
dec_number = 0

Hex_letter = ['A','B','C','D','E','F']
Hex_number = [10,11,12,13,14,15]

for i in range(cipher):
    try:
        dec_number += int(a[i]) * (16 ** i)
    except:
        dec_number += Hex_number[Hex_letter.index(a[i])] * (16 ** i)

print(dec_number)

https://github.com/koreanraichu/Python/blob/master/Hex%20to%20Dec.py

 

GitHub - koreanraichu/Python: Upload my file

Upload my file . Contribute to koreanraichu/Python development by creating an account on GitHub.

github.com

코드와 깃헙은 순서대로 2, 8, 16진수. 변환하는 원리도 대충 아실 듯 하지만... 16진수는 알파벳이 껴있어서 int로 변환이 안 되는 경우가 존재한다. 알파벳의 경우 알파벳 배열과 그에 상응하는 숫자 배열을 만들어서 인덱스 찾고 인덱싱해서 값 가져오게 했다. 16진수라 여섯개 고정이라 수동입력했고.