Untitled
unknown
plain_text
8 months ago
1.1 kB
8
Indexable
import csv
def build_bitmap(row):
# Determine if secondary bitmap is needed
used_des = [int(col.split('-')[1]) for col in row if col.startswith("DE-") and row[col].strip()]
has_secondary = any(de > 64 for de in used_des)
# 8 bytes for primary bitmap, 8 more for secondary if needed
bitmap = bytearray(16 if has_secondary else 8)
for de in used_des:
if de == 1:
continue # Bit 1 is reserved for "secondary bitmap present", set below
byte_index = (de - 1) // 8
bit_index = (de - 1) % 8
bitmap[byte_index] |= 1 << (7 - bit_index)
# Set bit 1 if secondary bitmap is used
if has_secondary:
bitmap[0] |= 1 << 7
return bytes(bitmap) # Return as immutable bytes object
# Example reading a CSV
with open("your_file.csv", newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
mti = row["MTI"]
bitmap = build_bitmap(row)
print(f"MTI: {mti} | Bitmap: {bitmap.hex().upper()}")
Editor is loading...
Leave a Comment