Reputation: 19
(Python-GColab IDE) Instructions: Make a program that turns the 8-bit data into the 12-digit codeword. The code should figure out what the four parity bits are and put them in the right places in the codeword.
The input could be a single letter, which could be in uppercase or lowercase. The output is the 12-digit codeword and the 3-letter hexadecimal equivalent of the codeword.
Note: (Even Parity, 12 number of bits, 4 bit parities, 8-data bits(add 0 at the left to make 8)
My Question:
Can I ask if how can I get the four bit parity given a character? I have my code already but I'm stuck in that part. My partial code is below.
x = True
while(x==True):
ASCII_input = str(input("Input a character: "))
if (ASCII_input) == 1:
ASCII_input = input("Input a correct character ")
else:
break
ASCII_code = ord(ASCII_input) #Char to ASCII
print("The ASCII value of " + ASCII_input + " is", ASCII_code)
#ASCII to 8 bit binary
bnr = bin(int(ASCII_code)).replace('0b','')
x = bnr[::-1] #this reverses an array
while len(x) < 8:
x += '0'
bnr = x[::-1]
print(bnr)
Upvotes: 0
Views: 382
Reputation: 31339
Although I fixed the indentation on your loop, it doesn't actually do what you seem to think it does - ASCII_input
would never be 1
, since it's the str
entered by the user (the conversion with str()
doesn't do anything either, nor do the parentheses around it in the if
statement). The rest of the code gets and prints the ordinal value of the character (it's ASCII value for entered characters in that range - but not all characters are ASCII characters if you don't speak English). And then prints the 8-bit representation of that value (which you convert to an integer, even though it already is an integer). For padding a string with zeroes, you can use .zfill()
.
Your code without the bits that don't do anything:
text = input("Input a character: ")
value = ord(text)
print("The ordinal value of " + text + " is", value)
bnr = bin(value)[2:]
print(bnr.zfill(8))
However, the code you provided doesn't actually do anything to answer the problem you were set: "make a program that turns the 8-bit data into the 12-digit codeword. The code should figure out what the four parity bits are and put them in the right places in the codeword."
From your title it's clear that the 4 bits should provide a Hamming error correcting code for the data - have you looked at the definition of Hamming codes at all and attempted to code anything of the sort?
Upvotes: 1