-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
49 lines (40 loc) · 1.66 KB
/
Copy pathapp.py
File metadata and controls
49 lines (40 loc) · 1.66 KB
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
from flask import Flask, request, jsonify
from flask_cors import CORS# DELETE IN PRODUCTION?
import reedsolo
# Initialize Flask application
app = Flask(__name__)
CORS(app)# DELETE IN PRODUCTION?
# Endpoint to process a bit string
@app.route('/qr-reed-solomon', methods=['POST'])
def qr_reed_solomon():
# Get the bit string from the request JSON data
data = request.get_json()
bit_string = data.get('bit_string')
if bit_string is None:
return jsonify({'error': 'Bit string is required in the request'}), 400
def binary_string_to_bytes(binary_string):
# Convert binary string to integer
value = int(binary_string, 2)
# Determine the number of bytes needed for the integer (ceil division by 8)
num_bytes = (len(binary_string) + 7) // 8
# Convert integer to bytes
byte_data = value.to_bytes(num_bytes, byteorder='big')
return byte_data
def bytes_to_binary_string(byte_array):
binary_string = ""
for byte in byte_array:
# Convert each byte to its binary representation (8 bits)
# Use '{:08b}'.format(byte) to ensure each byte is represented as 8 bits
binary_string += '{:08b}'.format(byte)
return binary_string
data = binary_string_to_bytes(bit_string)
# Initialize the Reed-Solomon encoder with the required parameters
rs = reedsolo.RSCodec(10) # 10 ECC bytes
# Encode the data
ecc_encoded = rs.encode(data)
bits_ecc = bytes_to_binary_string(ecc_encoded)
# Return the processed bit string in the response
return jsonify({'bits_ecc': bits_ecc})
# Run the application
if __name__ == '__main__':
app.run(debug=True)