-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw03.py
51 lines (43 loc) · 1.37 KB
/
hw03.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
CS1122 Homework 3
Austin Vuong N10778545 aav304
"""
def q2 (n): # Convert a number to binary
binary_num = "{0:08b}".format(n)
print (binary_num)
q2(57)
q2(123)
q2(85)
q2(38)
def q3 (hex_nums): # Convert a list of hex numbers into a string
lst = hex_nums.split(' ')
for item in lst:
item = str(item)
value = eval(item)
char = chr(value)
print (char, end='')
print ()
q3('0x41 0x53 0x43 0x49 0x49 0x20 0x77 0x68 0x61 0x74 0x20 0x79 0x6f 0x75 0x20 0x64 0x69 0x64 0x20 0x74 0x68 0x65 0x72 0x65')
q3('0x39 0x41 0x4d 0x20 0x69 0x73 0x20 0x74 0x6f 0x6f 0x20 0x65 0x61 0x72 0x6c 0x79 0x20 0x66 0x6f 0x72 0x20 0x63 0x6c 0x61 0x73 0x73')
q3('0x43 0x6f 0x6d 0x70 0x75 0x74 0x65 0x72 0x73 0x20 0x61 0x72 0x65 0x20 0x6d 0x61 0x67 0x69 0x63')
q3('0x57 0x68 0x61 0x74 0x20 0x74 0x68 0x65 0x20 0x68 0x65 0x78 0x3f')
def q4 (bin_num): # Convert a binary number into a hex number
hex_num = hex(int(bin_num, 2))
print (hex_num)
q4('00001011101011101101111010101101')
q4('11001010111111101111101011001110')
q4('10111110111011111101000000001101')
q4('11111111111111111001000001100010')
def q5 (n): # Convert octal to decimal
n_string = str(n)
dec_num = 0
counter = 0
for i in n_string:
counter += 1
i_int = int(i)
dec_num += i_int * (8 ** (len(n_string) - counter))
print (dec_num)
q5(10)
q5(42)
q5(77)
q5(113)