def encodeLongitude(lon_float): """Input longitude in decimal format, and produce binary representation Args: lon_float (float): longitude in decimal format Returns lon_binary (str): binary string representation of longitude """ ##lon_float must contain 5 decimal places lon_float = float("{:.5f}".format(lon_float)) ##For degrees, take whole number and convert to 8 bit binary string lon_degrees = int(lon_float) lon_degrees_bin = str(decodefunctions.dec2bin(lon_degrees).zfill(8)) ##Decimal portion - Integral Number Conversion Method lon_decimal = (lon_float - lon_degrees) lon_decimal_bin = '' lon_decimal = lon_decimal * pow(2, 15) lon_decimal = int(round(lon_decimal)) lon_decimal_bin = decodefunctions.dec2bin(lon_decimal).zfill(15) lon_binary = lon_degrees_bin + lon_decimal_bin return lon_binary
def encodelongFGB(hex_code, latitude, southnorth, longitude, eastwest, suppdata): latitude = round(float(latitude) * 1000, 0) longitude = round(float(longitude) * 1000, 0) c = decodehex2.BeaconFGB() try: c.processHex(str(hex_code.strip())) if c.protocolflag() == 'User': binstr = c.bin[0:25] + '1' + c.bin[26:86] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata binstr = binstr + str(southnorth) + dec2bin(int(float(latitude/1000)),7) + \ dec2bin( round(( float(latitude/1000) - int(float(latitude/1000))) * 15,0) ,4) + str(eastwest) + \ dec2bin(int(float(longitude / 1000)), 8) + \ dec2bin(round((float(longitude / 1000) - int(float(longitude / 1000))) * 15, 0), 4) bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() in [ 'Standard Location', 'Standard Location Protocol - Test' ]: bincoord = stdloc(latitude, longitude) binstr = c.bin[0:25] + '1' + c.bin[26:65] + str( southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() in [ 'ELT-DT Location', 'RLS Location' ]: bincoord = eltdt_rls(latitude, longitude) binstr = c.bin[0:25] + '1' + c.bin[26:67] + str( southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype( ) == 'National Location': bincoord = natloc(latitude, longitude) binstr = c.bin[0:25] + '1' + c.bin[26:59] + str( southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[ 3] + '000000' bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 return bin2hex(binstr[1:]) except decodehex2.HexError as e: print(e.value, e.message)
def pdf1_to_bch1(pdf1, bch1): #valid pdf1 binary_data_pdf1 = newdata = pdf1 correctedbch1 = bch1 bch1 = bch1 + '000' #print('valid pdf1',len(binary_data_pdf1),binary_data_pdf1) BCH_POLYNOMIAL = 137 #137 BCH_BITS = 3 bitflips = 0 bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) data = bytearray(bitstring_to_bytes(binary_data_pdf1)) print('m:', bch.m) rebuildpdf = '' for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf = rebuildpdf + segment print(e, data[e], segment) #print(binary_data_pdf1) print(rebuildpdf, len(rebuildpdf), binary_data_pdf1 == rebuildpdf) ecc = bch.encode(data) print(len(ecc)) bchstring = '' for e in ecc: binchar = decodefunctions.dec2bin(e) print(e, binchar) bchstring = bchstring + binchar # create array of ecc provide by bch ecc_provided = bytearray(bitstring_to_bytes(bch1)) packet = data + ecc_provided bchstr2 = '' print(len(data), len(ecc), len(packet)) print('ecc included:', ecc_provided, len(ecc_provided), type(ecc_provided)) print('ecc calc:', ecc, len(ecc), type(ecc)) print('match', ecc == ecc_provided) if ecc != ecc_provided: #packet = data + ecc data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] #correct bitflips = bch.decode_inplace(data, ecc) print('bitflips: %d' % (bitflips)) newdata = decodefunctions.dec2bin(data[0]) for e in data[1:]: binchar = decodefunctions.dec2bin(e).zfill(8) #print(e, binchar) newdata = newdata + binchar correctedbch1 = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) #print(e, binchar) correctedbch1 = correctedbch1 + binchar if (len(correctedbch1)) > 21: correctedbch1 = correctedbch1[:21] return (bitflips, newdata, correctedbch1)
def correct_bchsgb(testhex): pdf = decodefunctions.hextobin(testhex)[:204] bchsgb = (decodefunctions.hextobin(testhex))[204:] binary_data_pdf = newdata = pdf.zfill(204) correctedbch = bchsgb BCH_POLYNOMIAL = 285 BCH_BITS = 6 bitflips = 0 bch = bchlibcaronoff.BCH(BCH_BITS, m=8, prim_poly=BCH_POLYNOMIAL) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 + 1 data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf)) rebuildpdf = '' for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf = rebuildpdf + segment rebuildpdf = rebuildpdf.zfill(204) ecc = bch.encode(data) bchstring = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) bchstring = bchstring + binchar ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) packet = data + ecc_provided if ecc != ecc_provided: data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] bch.data_len = max_data_len nerr = bch.decode(data, ecc) bitflips = nerr bch.correct(data, ecc) newdata = decodefunctions.dec2bin(data[0]) for e in data[1:]: binchar = decodefunctions.dec2bin(e).zfill(8) newdata = newdata + binchar newdata = newdata.zfill(204) correctedbch = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) correctedbch = correctedbch + binchar newhex = decodefunctions.bin2hex(newdata + correctedbch) else: bitflips = 0 newdata = pdf correctedbch = bchsgb newhex = decodefunctions.bin2hex(pdf + bchsgb) return (bitflips, newdata, correctedbch, newhex)
def encodelong(form, h): secbin=dec2bin(int(form.timesecond.data),17) if int(form.altitude.data) >15952: alt='1'*10 else: alt= dec2bin(round((int(form.altitude.data)+400)/float(16)),10) completebin = form.longSGB(h) + '0001' + secbin+alt + form.act.data + form.gnss.data+form.battery.data + '0'*9 bch = calcBCH(completebin, 0, 202, 250) return bin2hex('00' + completebin + bch)
def stdloc(lat,long): latdegrees = round(float(lat / 1000) * 4, 0) binlat = dec2bin(latdegrees, 9) longdegrees = round(float(long / 1000) * 4, 0) binlong = dec2bin(longdegrees, 10) offsets = [] for coord in [(lat, latdegrees), (long, longdegrees)]: dif = float(coord[0] / 1000) - float(coord[1] / 4) offsets.append(min_sec(dif, 5, 4)) return (binlat, binlong, offsets[0], offsets[1])
def natloc(l,lg): lat=float(l/1000) long=float(lg/1000) binlat = dec2bin(int(lat),7) + dec2bin( round(float((lat-int(lat))*30 ),0 ), 5) binlong = dec2bin(int(long), 8) + dec2bin(round(float((long - int(long)) * 30), 0), 5) offsets=[] for coord in [l,lg]: print(int(coord/1000),float(coord/1000)-int(coord/1000), round(30*(float(coord/1000) - int(coord/1000)) ,0)/30 ) rnd= float(coord/1000)-int(coord/1000) - round(30*(float(coord/1000) - int(coord/1000)) ,0)/30 offsets.append(min_sec(rnd,2,4)) return (binlat,binlong,offsets[0],offsets[1])
def eltdt_rls(lat,long): latdegrees = round(float(lat / 1000) * 2, 0) binlat = dec2bin(latdegrees, 8) longdegrees = round(float(long / 1000) * 2, 0) binlong = dec2bin(longdegrees, 9) offsets=[] for coord in [(lat,latdegrees),(long,longdegrees)]: dif = float(coord[0] / 1000) - float(coord[1] / 2) offsets.append(min_sec(dif, 4, 4)) return (binlat,binlong,offsets[0],offsets[1])
def min_sec(rnd,bits_min, bits_sec): if (rnd) > 0: s = '1' else: s = '0' dif=abs(rnd) minutes =int(float(dif*60)) minbin = dec2bin(minutes, bits_min) seconds = float(dif*3600) - float(minutes*60) sec = round(float(seconds/4)) secbin = dec2bin(sec, bits_sec) print(minutes,sec) return s+minbin+secbin
def encodeLatitude(lat_float): """Input latitude in decimal format, and produce binary representation Args: lat_float (float): latitude in decimal format Returns lat_binary (str): binary string representation of latitude """ ##lat_float must contain 5 decimal places lat_float = float("{:.5f}".format(lat_float)) ##For degrees, take whole number and convert to 7 bit binary string lat_degrees = int(lat_float) lat_degrees_bin = str(decodefunctions.dec2bin(lat_degrees).zfill(7)) ##Decimal portion lat_decimal = (lat_float - lat_degrees) lat_decimal_bin = '' ##Successive Multiplication Method """ lat_decimal *= 2 while len(lat_decimal_bin) < 15: lat_decimal = float("{:.5f}".format(lat_decimal)) print lat_decimal print lat_decimal_bin if lat_decimal > 1: lat_decimal_bin += '1' lat_decimal -= 1 else: lat_decimal_bin += '0' lat_decimal *= 2 ##If remainder >= 0.5, we must add 1 to the computed binary number if lat_decimal >= 0.5: print 'adding one' lat_remainder = int(bin2dec(lat_decimal_bin))+ 1 lat_decimal_bin = decodefunctions.dec2bin(lat_remainder).zfill(15) print lat_decimal_bin """ ##Integral Number Conversion Method lat_decimal = lat_decimal * pow(2, 15) lat_decimal = int(round(lat_decimal)) lat_decimal_bin = decodefunctions.dec2bin(lat_decimal).zfill(15) lat_binary = lat_degrees_bin + lat_decimal_bin return lat_binary
def make_full(hexinput): pdf1 = decodefunctions.hextobin(hexinput) b = pdf1[:204] + '0' * 48 print(b, len(b)) ecc = calcBCH(b, 0, 202, 250) newhex = decodefunctions.bin2hex(pdf1[:204] + ecc) print(newhex) print(ecc, decodefunctions.bin2hex(ecc)) BCH_POLYNOMIAL = 285 # 285 BCH_BITS = 6 bitflips = 0 bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 data = bytearray(bch1correct.bitstring_to_bytes(pdf1[:204])) necc = bch.encode(data) bchstring = '' for e in necc: binchar = decodefunctions.dec2bin(e).zfill(8) bchstring = bchstring + binchar print(bchstring, decodefunctions.bin2hex(bchstring)) newhex = decodefunctions.bin2hex(pdf1[:204] + bchstring) return newhex
def pdf2_to_bch2(pdf2, bch2): binary_data_pdf2 = newdata = pdf2.zfill(26) correctedbch2 = bch2 bch2 = bch2 + '0000' BCH_POLYNOMIAL = 67 BCH_BITS = 2 bitflips = 0 #bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) bch = bchlibcaronoff.BCH(2, prim_poly=67) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf2)) rebuildpdf2 = '' for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf2 = rebuildpdf2 + segment ecc = bch.encode(data) # create array of ecc provide by bch ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bch2)) packet = data + ecc_provided # # if ecc != ecc_provided: data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] # #correct nerr = bch.decode(data, ecc) bitflips = nerr #bitflips = bch.decode_inplace(data,ecc) bch.correct(data, ecc) packet = data + ecc newdata = decodefunctions.dec2bin(data[0]) #.zfill(2) for e in data[1:]: binchar = decodefunctions.dec2bin(e).zfill(8) newdata = newdata + binchar # correctedbch2 = '' #decodefunctions.dec2bin(ecc[0]) for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) correctedbch2 = correctedbch2 + binchar return (bitflips, newdata.zfill(26), correctedbch2[:-4])
def samplepdf2(f, r): for n in range(r): pdf2 = randombinary(26).zfill(26) bch2 = Fcn.calcbch('0' * 107 + pdf2, '1010100111001', 107, 133, 145) + '0000' bch = bchlib.BCH(67, 2) data = bytearray(bch1correct.bitstring_to_bytes(pdf2)) ecc = bch.encode(data) bchstring = (Fcn.dec2bin(ecc[0]).zfill(8) + Fcn.dec2bin(ecc[1]).zfill(8))[:12] + '0000' e = random.sample(range(0, 38), 3) scramble = list(pdf2 + bch2) for i in e: scramble[i] = str(int(not int(scramble[i]))) corrupt = ''.join(scramble) ecc_provided = bytearray(bch1correct.bitstring_to_bytes(corrupt[26:])) data_provided = bytearray(bch1correct.bitstring_to_bytes(corrupt[:26])) ecc_c = bch.encode(data_provided) print(ecc_c == ecc_provided, ecc, ecc_provided) packet = bytearray(bch1correct.bitstring_to_bytes( corrupt[:26])) + bytearray( bch1correct.bitstring_to_bytes(corrupt[26:])) data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] bitflips = bch.decode_inplace(data, ecc) newdata = Fcn.dec2bin(data[0]).zfill(2) for e in data[1:]: binchar = Fcn.dec2bin(e).zfill(8) newdata = newdata + binchar correctedbch2 = '' # decodefunctions.dec2bin(ecc[0]) for e in ecc: binchar = Fcn.dec2bin(e).zfill(8) correctedbch2 = correctedbch2 + binchar correctedbch2 = correctedbch2[:-4] bch2 = bch2[:-4] bchstring = bchstring[:-4] f.writelines([ '{},{},{},{},{},{},{},{}'.format( pdf2 + bch2, bch2 == bchstring, corrupt, len(pdf2 + bch2), corrupt == pdf2 + bch2, newdata + correctedbch2, newdata + correctedbch2 == pdf2 + bch2, bitflips), '\n' ])
def test63hex(): data = bytearray(b'\x03' + os.urandom(25)) bch = bchlibcaronoff.BCH(6, m=8, prim_poly=285) ecc = bch.encode(data) rebuildpdf = '' for e in data: segment = decodefunctions.dec2bin(e).zfill(8) rebuildpdf = rebuildpdf + segment rebuildbch = '' for e in ecc: segment = decodefunctions.dec2bin(e).zfill(8) rebuildbch = rebuildbch + segment b = rebuildpdf + rebuildbch #print(decodefunctions.bin2hex(b)) return decodefunctions.bin2hex(b)
def encodelong(form, h): hoursbin = dec2bin(int(form.hours.data), 6) if form.minutes.data == None: minbin = dec2bin(2047, 11) else: minbin = dec2bin(int(form.minutes.data), 11) if form.altitude.data == None: altbin = '1' * 10 else: a = round((float(form.altitude.data + 400) / 16), 0) altbin = dec2bin(a, 10) completebin = form.longSGB( h ) + '0000' + hoursbin + minbin + altbin + form.hdop.data + form.vdop.data + form.act.data + form.bat.data + form.fix.data + '00' bch = calcBCH(completebin, 0, 202, 250) print(completebin) return bin2hex('00' + completebin + bch)
def pdf_to_bchsgb(pdf, bchsgb): binary_data_pdf = newdata = pdf.zfill(204) print(len(binary_data_pdf)) correctedbch = bchsgb bchsgb = bchsgb BCH_POLYNOMIAL = 285 #285 BCH_BITS = 6 bitflips = 0 bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf)) print('len in bytes of data', len(data)) print('m------------:', bch.m) print('ecc_bits: {} ecc_bytes: {} '.format(bch.ecc_bits, bch.ecc_bytes)) print('m:', bch.m) print('n: {} bytes: {}'.format(bch.n, bch.n // 8)) print('max data length:', max_data_len) print('t: {}'.format(bch.t)) print('length of data in bytes:', len(data)) rebuildpdf = '' print('---Per Byte' + '-' * 40) for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf = rebuildpdf + segment print(e, data[e], segment) print('-' * 50) print('pdf as input', len(pdf), pdf) print('bch as input', len(bchsgb), bchsgb) print('-' * 50) rebuildpdf = rebuildpdf.zfill(202) print(rebuildpdf, len(rebuildpdf)) print(binary_data_pdf == rebuildpdf) print('inputed data len to encode bch: ', len(data)) ecc = bch.encode(data) print('computed ecc bytes:', len(ecc)) bchstring = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) print(e, binchar) bchstring = bchstring + binchar print(bchsgb) print(bchstring) print(bchsgb == bchstring) print(decodefunctions.bin2hex(rebuildpdf + bchstring)) # create array of ecc provide by bch ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) print(ecc) print('ecc included:', ecc_provided, len(ecc_provided), type(ecc_provided)) print('ecc calc:', ecc, len(ecc), type(ecc)) print('provided', bchsgb, len(bchsgb), decodefunctions.bin2hex(bchsgb)) print('calculated', bchstring, len(bchstring), decodefunctions.bin2hex(bchstring)) packet = data + ecc_provided print('ecc equiv', ecc == ecc_provided) if ecc != ecc_provided: data, ecc = packet[1:-bch.ecc_bytes], packet[-bch.ecc_bytes:] print('cropped len data', len(data)) #data, ecc = packet[:-6], packet[-6:] print(data, ecc) bitflips = bch.decode_inplace(data, ecc) print('bitflips: %d' % (bitflips)) newdata = pdf[:4] #decodefunctions.dec2bin(byte_one) for e in data: binchar = decodefunctions.dec2bin(e).zfill(8) newdata = newdata + binchar # correctedbch = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) correctedbch = correctedbch + binchar return (bitflips, newdata.zfill(204), correctedbch[:48])
def encodelongFGB(hex_code, formdata): for e in formdata: print('form control' , e, formdata[e]) c = decodehex2.BeaconFGB() pad24='1'*16 +'000101111' try: c.processHex(str(hex_code.strip())) if c.loctype()!='National User': latitude = round(float(formdata['latitude']) * 1000, 0) longitude = round(float(formdata['longitude']) * 1000, 0) southnorth = formdata['northsouth'] eastwest = formdata['eastwest'] if c.protocolflag() == 'User' and c.loctype()!='National User': suppdata= formdata['encodepos'] binstr = pad24 + '1' + c.bin[26:86] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata binstr = binstr + str(southnorth) + dec2bin(int(float(latitude/1000)),7) + \ dec2bin( round(( float(latitude/1000) - int(float(latitude/1000))) * 15,0) ,4) + str(eastwest) + \ dec2bin(int(float(longitude / 1000)), 8) + \ dec2bin(round((float(longitude / 1000) - int(float(longitude / 1000))) * 15, 0), 4) bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.loctype() == 'National User': binstr = '_'+ '1'*15 + '000101111' + '1' + c.bin[26:86] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 +'0' *26 bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() in \ ['Location: Standard Location Protocol - ELT (Serial)','Standard Location','Standard Location Protocol - Test','Standard Location Protocol - PLB (Serial)','Standard Location Protocol - EPIRB (Serial)','Std Loc. Serial ELT - Aircraft Operator Designator Protocol']: bincoord = stdloc(latitude, longitude) suppdata = '1101' + formdata['encodepos'] + formdata['auxdevice'] binstr = pad24 + '1' + c.bin[26:65] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() == 'RLS Location Protocol': # bit 115=132 are 18 bit location offset suppdata = formdata['encodepos'] + \ formdata['auxdevice'] + \ formdata['rlmtypeone'] + \ formdata['rlmtypetwo'] + \ formdata['feedbacktype1'] + \ formdata['feedbacktype2'] + \ formdata['rlsprovider'] bincoord = eltdt_rls(latitude, longitude) binstr = pad24 + '1' + c.bin[26:67] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() == definitions.ELT_DT_LOC: suppdata = formdata['meansactivation'] + formdata['encodedaltitude'] + formdata['freshness'] aircraft3letter = formdata['aircraft_3ld'] if suppdata[-2:] != '00': # bit 115=132 are 18 bit location offset bincoord= eltdt_rls(latitude, longitude) binstr ='_'+ '1'*15 + '000101111' + '1' + c.bin[26:67] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 else: # bits 115-132 are aircraft 3 letter designators 000 and 3 segments of 5 bit modified baudot for each letter designator bincoord = eltdt_rls(latitude, longitude) binstr = '_' + '1'*15 + '000101111' + '1' + c.bin[26:67] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) aircraft3letterbin='' for letter in aircraft3letter.strip(): try: key = next(key for key, value in definitions.baudot.items() if value == letter.upper()) except StopIteration: key = '111111' key = key[1:] aircraft3letterbin = aircraft3letterbin + key aircraft3letterbin = aircraft3letterbin + '11111' * (3 - len(aircraft3letter.strip())) binstr = binstr + bch1 + suppdata + '000' + aircraft3letterbin bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() == 'National Location': bincoord= natloc(latitude, longitude) suppdata = '110' + formdata['nationalassign']+ formdata['encodepos'] + formdata['auxdevice'] binstr = pad24 + '1' + c.bin[26:59] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) if formdata['nationalassign'] == '0': refine = '0'*14 else: refine = bincoord[2] + bincoord[3] binstr = binstr + bch1 + suppdata + refine + '000000' bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 return bin2hex(binstr[1:]) except decodehex2.HexError as e: print(e.value, e.message)
def encodelong(form, h): nat= str(dec2bin(int(form.national_use.data))).zfill(44) completebin = form.longSGB(h) + '0011' + nat bch = calcBCH(completebin, 0, 202, 250) return bin2hex('00' + completebin + bch)
##BIT 1-20 Type Approval Certificate # # Ask for the user input and store it in userInput while next_step == False: try: userInput = raw_input('\nPlease enter TAC (0 to 1,048,575): ') or '1' except EOFError: userInput = 0 try: tac = int(userInput) # Catch the exception if the input was not a number except ValueError: print 'Error: value must be an integer' tac = 0 else: bits_tac = Func1.dec2bin(tac).zfill(20) if len(bits_tac) != 20: print 'Error: input too high.' else: break printtxt('TAC# : {} - binary: {}\n'.format(str(Func2.bin2dec(bits_tac)),bits_tac)) ##BIT 21-30 Serial Number while next_step == False: try: userInput = raw_input('\nPlease enter beacon serial number (0 to 1023): ') or '1' except EOFError: userInput = 0 try:
def msgbits_to_bch(msgbits,bchsgb): print(len(msgbits)) binary_data_msgbits = newdata = msgbits.zfill(207) #.zfill(204) #Msg size is 202 correctedbch=bchsgb BCH_POLYNOMIAL = 463 BCH_BITS = 6 bitflips=0 #109,113,127,131,137,139,149,151,157,163,167,173,179, 181, 191, 193, 197, 199, 211, 223, 227, 229 #233, 239 , 241, 251, 257, 263, 269, 271, 277, 281 for p in [229,233,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389, 397,401, 409, 419, 421, 431,433,439,443,449,457, 461,463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541,547,557,563,569,571,577,587,594,599,601,607]: # 233, 239 , 241, 251, 257, 263, 269, 271, 277, 281, # 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, # 353, 359, 367, 373 , 379, 383 , 389, 397 , 401, 409, # 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, # 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, # 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, # 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, # 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, # 739, 743, 751, 757, 761, 769, 773, 787, 797, 809]: try: bch = bchlib.BCH(p, BCH_BITS) data = bytearray(bch1correct.bitstring_to_bytes(binary_data_msgbits)) #data = data[1:] rebuildmsg='' for e in range(len(data)): segment=Fcn.dec2bin(data[e]).zfill(8) rebuildmsg=rebuildmsg+segment #print(e, data[e],segment) #print(binary_data_msgbits,len(binary_data_msgbits)) #print(rebuildmsg,len(rebuildmsg),binary_data_msgbits==rebuildmsg) ecc = bch.encode(data) print('computed ecc bytes:',len(ecc)) ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) bchstring = ecc_provided[0] print(ecc) print(ecc_provided) for e in ecc_provided: binchar = Fcn.dec2bin(e).zfill(8) print(e, int(e), binchar) #bchstring = bchstring + binchar for e in ecc: #binchar = Fcn.dec2bin(e).zfill(8) print(e, int(e) , binchar) #bchstring = bchstring + binchar #print(bchstring) #print(bchsgb) print(p) except RuntimeError: pass # create array of ecc provide by bch ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) packet=data + ecc_provided print(len(data),len(ecc),len(packet)) print('ecc included:',ecc_provided,len(ecc_provided),type(ecc_provided)) print('ecc calc:', ecc,len(ecc),type(ecc)) # # # if ecc!=ecc_provided: data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] # #correct bitflips = bch.decode_inplace(data,ecc) print('bitflips: %d' % (bitflips)) newdata=Fcn.dec2bin(data[0]) for e in data[1:]: binchar = Fcn.dec2bin(e).zfill(8) # #print(e, binchar) newdata = newdata + binchar # correctedbch = '' #decodefunctions.dec2bin(ecc[0]) for e in ecc: binchar = Fcn.dec2bin(e).zfill(8) # #print(e, binchar) correctedbch = correctedbch + binchar return (bitflips,newdata,correctedbch)
def byte_to_binary(binarray): s='' for b in binarray: s=s+decodefunctions.dec2bin(b) return s
def pdf2_to_bch2(pdf2, bch2): #valid pdf1 binary_data_pdf2 = newdata = pdf2 correctedbch2 = bch2 bch2 = bch2 + '0000' #print('valid pdf1',len(binary_data_pdf1),binary_data_pdf1) BCH_POLYNOMIAL = 67 #137 BCH_BITS = 2 bitflips = 0 bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf2)) rebuildpdf2 = '' for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf2 = rebuildpdf2 + segment #print(e, data[e],segment) #print(binary_data_pdf2) #print(rebuildpdf2,len(rebuildpdf2),binary_data_pdf2==rebuildpdf2) ecc = bch.encode(data) #print('computed ecc bytes:',len(ecc)) bchstring = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) #print(e, binchar) bchstring = bchstring + binchar #print(bchstring) #print(bch2) # create array of ecc provide by bch ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bch2)) packet = data + ecc_provided bchstr2 = '' #print(len(data),len(ecc),len(packet)) #print('ecc included:',ecc_provided,len(ecc_provided),type(ecc_provided)) #print('ecc calc:', ecc,len(ecc),type(ecc)) # # # if ecc != ecc_provided: data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] # #correct bitflips = bch.decode_inplace(data, ecc) #print('bitflips: %d' % (bitflips)) newdata = decodefunctions.dec2bin(data[0]) for e in data[1:]: binchar = decodefunctions.dec2bin(e).zfill(8) # #print(e, binchar) newdata = newdata + binchar # correctedbch2 = '' #decodefunctions.dec2bin(ecc[0]) for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) # #print(e, binchar) correctedbch2 = correctedbch2 + binchar if (len(correctedbch2)) > 12: correctedbch2 = correctedbch2[:12] return (bitflips, newdata.zfill(26), correctedbch2)