def base_converter(n, base):
if not (1 < base < 37): raise ValueError("invalid base value")
chars = [chr(i) for i in range(48, 58)] + [chr(i) for i in range(65, 91)]
is_negative = n < 0
n = abs(n)
result = []
while n > 0:
result.append(chars[n%base])
n //= base
return ("-" if is_negative else "") + "".join(result[::-1])
print(base_converter(-200, 16))
파이썬에서 내장된 bin, oct, hex를 넘어 2진수부터 36진수까지 변환이 가능한 코드이다.
음수 표기는 위에서 파이썬에 내장된 함수처럼 앞에 -가 붙어 나온다.
'개발' 카테고리의 다른 글
| Blob 이모지 테스트 :blobaww: (0) | 2025.12.17 |
|---|---|
| [파이썬] JSON 사용 시 TypeError: Object of type bytes is not JSON serializable 에러 (0) | 2025.09.28 |
| 구글 앱 비밀번호 생성하기 (0) | 2025.08.15 |
| [파이썬] ඞ 어몽어스 캐릭터 출력하기!! (0) | 2025.06.18 |
| [파이썬] 내장 함수 round()에 대하여 (0) | 2025.05.18 |