[Python] bin, hex, oct ν•¨μˆ˜

MariGoldΒ·2023λ…„ 11μ›” 23일
0

Python

λͺ©λ‘ 보기
10/11
post-thumbnail

πŸ’‘bin ν•¨μˆ˜πŸ’‘

주어진 수λ₯Ό 2μ§„μˆ˜λ‘œ λ³€ν™˜ν•œλ‹€. λ°˜ν™˜λœ κ°’ μ•žμ— '0b'κ°€ λΆ™μœΌλ―€λ‘œ λ³€ν™˜λœ κ°’λ§Œ μ‚¬μš©ν•˜λ €λ©΄ [2:]λ₯Ό ν•¨μˆ˜ 뒀에 뢙이면 λœλ‹€.

πŸ’‘μ˜ˆμ‹œ μ½”λ“œπŸ’‘

a = 100

>>> print(bin(a))
0b1100100

>>> print(bin(a)[2:])
1100100

πŸ’‘hex ν•¨μˆ˜πŸ’‘

주어진 인자λ₯Ό 16μ§„μˆ˜λ‘œ λ³€ν™˜ν•œλ‹€. λ°˜ν™˜λœ κ°’ μ•žμ— '0x'κ°€ λΆ™μœΌλ―€λ‘œ λ³€ν™˜λœ κ°’λ§Œ μ‚¬μš©ν•˜λ €λ©΄ [2:]λ₯Ό ν•¨μˆ˜ 뒀에 뢙이면 λœλ‹€.

πŸ’‘μ˜ˆμ‹œ μ½”λ“œπŸ’‘

a = 100

>>> print(hex(a))
0x64

>>> print(hex(a)[2:])
64

πŸ’‘oct ν•¨μˆ˜πŸ’‘

주어진 인자λ₯Ό 8μ§„μˆ˜λ‘œ λ³€ν™˜ν•œλ‹€. λ°˜ν™˜λœ κ°’ μ•žμ— '0o'κ°€ λΆ™μœΌλ―€λ‘œ λ³€ν™˜λœ κ°’λ§Œ μ‚¬μš©ν•˜λ €λ©΄ [2:]λ₯Ό ν•¨μˆ˜ 뒀에 뢙이면 λœλ‹€.

πŸ’‘μ˜ˆμ‹œ μ½”λ“œπŸ’‘

a = 100

>>> print(oct(a))
0o144

>>> print(oct(a)[2:])
144

πŸ’‘kμ§„μˆ˜ λ³€ν™˜πŸ’‘

bin, hex, octλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  λ‹€μŒκ³Ό 같은 λ°©λ²•μœΌλ‘œ kμ§„μˆ˜λ‘œ λ³€ν™˜ν•  수 μžˆλ‹€.

data = '0123456789ABCDEF'
def change(num, k) :
	answer = ''
	while num > 0 :
		num, m = divmod(num, k)
    	answer += str(data[m])
	return answer = answer[::-1]

πŸ’‘μ˜ˆμ‹œ μ½”λ“œπŸ’‘

def change(num, k) :
	answer = ''
	while num > 0 :
		num, m = divmod(num, k)
    	answer += str(data[m])
	return answer[::-1]

>>> change(100, 5)
400

>>> change(10, 12)
A
profile
μ‘°κΈˆμ”© μ•žμœΌλ‘œ λ‚˜μ•„κ°€λŠ” μ‹ μž… 개발자 :)

0개의 λŒ“κΈ€