์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

ํŒŒ์ด์ฌ(Python) 2์ง„์ˆ˜ 8์ง„์ˆ˜ 16์ง„์ˆ˜ ๋ณ€ํ™˜ํ•˜๊ธฐ (int, hex, oct, bin)

  • -

 

 

 

* ์ ‘๋‘์–ด

์ผ์ƒ์ ์œผ๋กœ ์“ฐ๋Š” 10์ง„์ˆ˜๋Š” ์ ‘๋‘์–ด๊ฐ€ ํ•„์š” ์—†์ง€๋งŒ, 2์ง„์ˆ˜, 8์ง„์ˆ˜, 16์ง„์ˆ˜๋Š” ์ ‘๋‘์–ด๊ฐ€ ๋ถ™๊ณ  ๋’ค์— ๊ทธ ํ˜•ํƒœ์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๊ฐ€ ๋ถ™๋Š”๋‹ค.

*2์ง„์ˆ˜ : 0b     (binary number)

*8์ง„์ˆ˜ : 0o     (octal number)

*16์ง„์ˆ˜ : 0x     (hexadecimal number)

๋นจ๊ฐ„๊ธ€์”จ๋Š” ํ•จ์ˆ˜๋ช…์ด ๋˜๋ฏ€๋กœ, ๊ธฐ์–ตํ•ด์•ผํ•œ๋‹ค. bin ,oct, hex

 

 


 

* ๋ฐฉ๋ฒ• 1  - int , bin , oct , hex ๋‚ด์žฅํ•จ์ˆ˜ ์ด์šฉํ•˜๊ธฐ

 

 

2์ง„์ˆ˜๋ฅผ 10์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
n_2 = '0b110010'
n_10 = int(n_2,2) #n_2๋Š” 2์ง„์ˆ˜์ด๋ฏ€๋กœ, 2๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
print(n_10)

>>>>>>>>>>  50

0b๋ฅผ ๋ถ™์—ฌ์ค˜์•ผ๋งŒ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋ถˆํŽธํ•˜๋‹ค.

 

10์ง„์ˆ˜๋ฅผ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
n_10 = 173
n_2  = bin(n_10)
print(n_2)

>>>>>>>>>>>> 0b10101101

 


 

8์ง„์ˆ˜๋ฅผ10์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
n_8 = '0o134134'
n_10 = int(n_8,8) # n_8, 8์ง„์ˆ˜
print(n_10)

>>>>>>>>>>>>>> 47196

 

10์ง„์ˆ˜๋ฅผ 8์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

 

n_10 = 173
n_8 = oct(n_10)
print(n_8)

>>>>>>>>>>>>>> 0o255

 

 


16์ง„์ˆ˜๋ฅผ 10์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
n_16 = '0xc1251'
n_10 = int(n_16,16)
print(n_10)

>>>>>>>>>>  791121
10์ง„์ˆ˜๋ฅผ 16์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
n_10 = 193
n_16 = hex(n_10)
print(n_16)

>>>>>>>>>> 0xc1

 

 

* ๋ฐฉ๋ฒ• 2  - format ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ

 

format(์ธ์ž๊ฐ’, '์ ‘๋‘์–ด')๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

10์ง„์ˆ˜  -> 2์ง„์ˆ˜, 8์ง„์ˆ˜, 16์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
num = 1514

num_2 = format(num,'b')  #2์ง„์ˆ˜
num_8 = format(num,'o')  #8์ง„์ˆ˜
num_16 = format(num,'x') #16์ง„์ˆ˜

print(num_2,num_8,num_16)

๊ฒฐ๊ณผ๊ฐ’ >>>>>>>>>>>>>>>>>>>>>>>>> 10111101010 2752 5ea

 

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.