Annexes

Fonctions

→ Codage Décimal vers Binaire

→ Décodage Binaire vers Décimal

→ Conversion d’un nombre entier en caractère

→ Conversion d’un caractere vers un entier

→ Calculer le bit de parité depuis un entier

→Extraction du code ASCII (suppression du bit de parité)

In [ ]:
# coding: utf-8

def codageBin(n):
    binaire=[]
    for i in range(8):
        quotient=n//2
        reste=n%2
        binaire.append(reste);
        n=quotient
    return binaire

def decodageBin(b):
    return b[7]*2**7+b[6]*2**6+b[5]*2**5+b[4]*2**4+b[3]*2**3+b[2]*2**2+b[1]*2**1++b[0]*2**0
        
def testParite(l_bin):
    nbre1=0
    for i in range(8):
        if l_bin[i]==1:
            nbre1+=1
    if nbre1%2==0:
        return True
    return False

def convCharToInt(caract):
    return ord(caract)

def convIntToChar(nbre):
    return chr(nbre)

def supprParite(nbre):
    if nbre>128:
        return nbre-128
    else:
        return nbre

def ajoutParite(nbre):
    codeBin=codageBin(nbre)
    if not testParite(codageBin(nbre)):
        codeBin[7]=1
    return decodageBin(codeBin)


caractere ='B'
integer=convCharToInt(caractere)
print(integer)
integer=ajoutParite(integer)
print(integer)
caractParite=convIntToChar(integer)
print(caractParite)

integer=convCharToInt(caractParite)
print(integer)
integer=supprParite(integer)
print(integer)
caractere=convIntToChar(integer)
print(caractere)

print("------------------------")

caractere ='C'
integer=convCharToInt(caractere)
print(integer)
integer=ajoutParite(integer)
print(integer)
caractParite=convIntToChar(integer)
print(caractParite)

integer=convCharToInt(caractParite)
print(integer)
integer=supprParite(integer)
print(integer)
caractere=convIntToChar(integer)
print(caractere)

print("------------------------")

print(supprParite(120))
print(supprParite(130))

print("------------------------")

b=print(codageBin(120))
b=print(codageBin(130))

print("------------------------")

print(testParite(codageBin(120)))
print(testParite(codageBin(130)))

print("------------------------")
      
print(ajoutParite(64))
print(ajoutParite(65))
      
print("------------------------")

Génération données erronées

In [ ]:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  generation_donnee_erronees.py
#  
#  Copyright 2019 Jérôme <jerome@Jerome>
#  
import sys
import random

citation_ok="All the world\'s a stage,And all the men and women merely players;\nThey have their exits and their entrances,\nAnd one man in his time plays many parts,\nHis acts being seven ages. At first, the infant,\nMewling and puking in the nurse\'s arms.\nThen the whining schoolboy, with his satchel\nAnd shining morning face, creeping like snail\nUnwillingly to school. And then the lover,\nSighing like furnace, with a woeful ballad\nMade to his mistress' eyebrow. Then a soldier,\nFull of strange oaths and bearded like the pard,\nJealous in honor, sudden and quick in quarrel,\nSeeking the bubble reputation\nEven in the cannon's mouth. And then the justice,\nIn fair round belly with good capon lined,\nWith eyes severe and beard of formal cut,\nFull of wise saws and modern instances;\nAnd so he plays his part. The sixth age shifts\nInto the lean and slippered pantaloon,\nWith spectacles on nose and pouch on side;\nHis youthful hose, well saved, a world too wide\nFor his shrunk shank, and his big manly voice,\nTurning again toward childish treble, pipes\nAnd whistles in his sound. Last scene of all,\nThat ends this strange eventful history,\nIs second childishness and mere oblivion,\nSans teeth, sans eyes, sans taste, sans everything."
newline_utf8 = "\n"
"""
print(citation_utf8)
print(sys.getdefaultencoding())
citation_ascii = citation_utf8.encode('ascii')
print(citation_ascii)

print("\n en utf8 : ",ord(newline_utf8))
newline_ascii = newline_utf8.encode('ascii')
print("\n en ASCII : ",ord(newline_ascii))

for i in range(10):
	a = citation_utf8[i]
	b = str(chr(citation_ascii[i]))
	print(chr(citation_ascii[i]),":",ord(a),"/",ord(b))

for i in range(len(citation_utf8)):
	if ord(citation_utf8[i]) >= 128:
		print("Attention : ",citation_utf8[i])
"""

citation_recu_CRC =""
citation_recu_sans_CRC = ""

#for i in range(len(citation_ok)):
for i in range(len(citation_ok)):
	#on met une erreur ou pas
	#on calcul le nombre de 1 pour déterminer le CRC
	nombrede1 = bin(ord(citation_ok[i])).count('1')
	if random.randint(0,100) < 10:
		#choix de la position de l'erreur
		lequel = random.randint(3,8)
		temp = bin(ord(citation_ok[i]))
		#on normalise à 8 bits
		temp = temp[:2] + '0' * (10-len(temp)) + temp[2-len(temp):]
		#print(temp,lequel,ord(citation_ok[i]),len(temp),int(temp,2))
		#introcuction de l'erreur en changeant la valeur du bit
		if temp[lequel] == '0':
			temp = temp[:lequel] + '1' + temp[lequel-9:]
			#print(temp,int(temp,2),"if")
		else:
			temp = temp[:lequel] + '0' + temp[lequel-9:]
			#print(temp,int(temp,2),"else")
	else:
		#pas d'erreur : rien à faire
		temp = bin(ord(citation_ok[i]))

	citation_recu_sans_CRC = citation_recu_sans_CRC + chr(int(temp,2))

	#on place le bit de parite
	#print(temp)
	if nombrede1 % 2:
		temp = temp[:2] + '1' + temp[3:]
		citation_recu_CRC = citation_recu_CRC + chr(int(temp,2))
	else:
		citation_recu_CRC = citation_recu_CRC + chr(int(temp,2))
	#print(citation_recu_CRC)
"""
print(citation_recu_CRC)
print()
print(citation_recu_sans_CRC)

for i in range(len(citation_recu_CRC)):
	if citation_recu_CRC[i] != citation_recu_sans_CRC[i]:
		print("i = ",i)
"""
f = open("citation_emise.txt",'w')
f.write(citation_ok)
f.close()
f = open("citation_recu.txt",'w')
f.write(citation_recu_sans_CRC)
f.close()
f = open("citation_recu_avec_CRC.txt",'w')
f.write(citation_recu_CRC)
f.close()