Skip to content

Instantly share code, notes, and snippets.

@Yaulendil
Created September 25, 2019 04:48
Show Gist options
  • Save Yaulendil/a7489274ee966ce129874ca10d4e85d0 to your computer and use it in GitHub Desktop.
Save Yaulendil/a7489274ee966ce129874ca10d4e85d0 to your computer and use it in GitHub Desktop.
Module for representation of bytes as UTF-16 Braille Characters for direct visualization of the bits.
from itertools import product
_hexd = "0123456789ABCDEF"
# Pairs of Hexadecimal Digits as Unicode Characters.
byte_pairs = tuple(map("".join, product(_hexd, _hexd)))
# Braille Characters. The Index of each Character is also its Integer Value.
# NOTE: This is NOT the order the Characters appear in Unicode. Due to legacy
# concerns, the bit pattern was made irregular when the fourth row was added.
# This order is modified to better represent bytes visually.
braille = (
"⠀⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇" # [00] - 0F
"⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏" # 10 - 1F
"⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗" # 20 - 2F
"⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟" # 30 - 3F
"⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧" # 40 - 4F
"⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯" # 50 - 5F
"⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷" # 60 - 6F
"⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿" # 70 - 7F (127)
"⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇" # 80 - 8F
"⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏" # 90 - 9F
"⢐⢑⢒⢓⢔⢕⢖⢗⣐⣑⣒⣓⣔⣕⣖⣗" # A0 - AF
"⢘⢙⢚⢛⢜⢝⢞⢟⣘⣙⣚⣛⣜⣝⣞⣟" # B0 - BF
"⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧" # C0 - CF
"⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯" # D0 - DF
"⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷" # E0 - EF
"⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿" # F0 - FF (255)
)
# Visual map of the bits in a Braille Byte:
# 1 16
# 2 32
# 4 64
# 8 128
# Braille Characters, unmodified.
bpoints = "".join(map(chr, range(0x2800, 0x2900)))
def bytes_to_braille(b: bytes) -> str:
hex_ = b.hex()
i = 0
s = ""
while i < len(hex_):
s += braille[int(hex_[i : i + 2], 16)]
i += 2
return s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment