개발/Python

[Python] - N진수 표현 및 Ascii

Dortmoot 2022. 7. 27. 18:07

Python 진법 변환


1. N Base -> 10 Base

  • value is A number or a string that can be converted into an integer number
  • base is A number representing the number format. Default value − 10

Syntax

- int(value, base)
# N base to 10 base
print(int('101',2))
> 5 
print(int('202',3))
> 20
print(int('ADF',16))
> 2783

 

2. 10 Base -> 2 / 8 / 16 Base

  • bin(), oct(), hex() function support 
# 2base
print(bin(11)[2:])
> 1011

# 8base
print(oct(11)[2:])
> 13

# 16base
print(hex(11)[2:])
> b

 

3. 10 Base -> N Base

  • change n base function Not supported
def solution(n,b):
    answer = ''
    while n>0 :
        n,mod = n//b , n%b
        answer+= str(mod)
        
    return answer[::-1]
    
# number,base = 10,2
# solution(number,base)

 

Python Ascii code


1. ord function

  • get character and return unicode int value
ord('a')
> 97

2. chr function

  • get int value and return unicode character value
chr(97)
> 'a'