10진수 16진수로 [파이썬]
언어/python2017. 10. 14. 14:45
이번엔 16진수다.
16진수 체계
0,1,2,3,4,...,9,A,B,C,D,E,F
입력
case 0
case 1 ~ F
case F보다 큰 수
16 123
7 11
낙서 ==============================================================
class FromDecimalToHex:
def __init__(self):
self.decimalValue = 0 # type of int
self.HexDict = {} # type of dictionary
self.qValue = 0 # 몫
self.rValue = 0 # 나머지
self.hexValue = "" # type of string
# step 1 ==================================================
def HexList_do_setting(self):
for i in range(0, 16):
if (i <= 9):
self.HexDict[i] = str(i)
else: # i >= 10
self.HexDict[i] = chr(i+55)
print ("hex_dictionary => {}".format(self.HexDict))
# step 2 ==================================================
def decimalValue_input(self):
while True:
try:
self.decimalValue = int(input("decimal input : "))
except:
print ("정수만 입력해주세요")
else:
if self.decimalValue < 0:
print ("Plz positive integer")
else:
print ("perfect")
break
# step 3 ==================================================
def fromDecimal_ToHex(self):
if (self.decimalValue == 0 or self.decimalValue == 1):
self.hexValue += self.HexDict[self.decimalValue]
else:
while True:
self.qValue = self.decimalValue//16
self.rValue = self.decimalValue%16
self.hexValue += self.HexDict[self.rValue]
if self.qValue < 16:
self.hexValue += self.HexDict[self.qValue]
break
else:
self.decimalValue = self.qValue
self.hexValue +="x0"
print (self.hexValue[::-1])
def main():
my1033 = FromDecimalToHex() # 객체 생성
my1033.HexList_do_setting()
my1033.decimalValue_input()
my1033.fromDecimal_ToHex()
if __name__ == "__main__":
main()
'언어 > python' 카테고리의 다른 글
엘라스틱 서치 :: 파이썬 (0) | 2017.10.27 |
---|---|
파이썬 1의 보수 만들기 (0) | 2017.10.16 |
10진수 8진수로 [파이썬] (0) | 2017.10.14 |
python turtle 슈퍼 그랑죠 피닉스 ㅋㅋㅋㅋㅋ 나도 오타쿠 인가 ㅋㅋ (0) | 2017.10.01 |
수정 중인 코드 (0) | 2017.09.24 |