#!/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()