Skip to content

Instantly share code, notes, and snippets.

@mwiemarc
Created November 5, 2019 11:18
Show Gist options
  • Save mwiemarc/b231077493a98ee47a1c32659c524cd1 to your computer and use it in GitHub Desktop.
Save mwiemarc/b231077493a98ee47a1c32659c524cd1 to your computer and use it in GitHub Desktop.
encode text into hex values to display as colors
local str =
'Es ist kein Geheimnis, dass das Markenzeichen der Defiasbande ihre roten Kopftücher sind. Aber wir haben herausgefunden, dass das Material, aus dem diese Kopftücher gemacht sind, den Rang des Bandenmitglieds bezeichnet. Jetzt, wo wir das wissen, möchte ich, dass Ihr so viele hochrangige Mitglieder der Defias eliminiert, wie Ihr könnt. Die Bandenmitglieder mit der höchsten Position findet Ihr zweifellos in van Cleefs Geheimversteck. Wenn Ihr fertig seid, bringt mir 10 rote Seidenkopftücher zum Beweis, dass sie tot sind, dann werde ich Euch belohnen. Viel Glück, <Name>.'
local byteStr = ''
for c in str:gmatch('[\0-\x7F\xC2-\xF4][\x80-\xBF]*') do
local b1, b2, b3, b4 = c:byte(1, -1) -- get all bytes for that character
local charStr
-- add bytes to string
charStr = b1 and tostring(b1) or charStr
charStr = b2 and string.format('%s/%d', charStr, b2) or charStr
charStr = b3 and string.format('%s/%d', charStr, b3) or charStr
charStr = b4 and string.format('%s/%d', charStr, b4) or charStr
byteStr = charStr and string.format('%s%s:', byteStr, charStr) or byteStr
end
byteStr = byteStr:gsub('[^\128-\191][\128-\191]*$', '') -- remove last char
local hexStr = ''
for c in byteStr:gmatch('.') do
local b = c:byte() - 46
hexStr = b and hexStr .. string.format('%X', b) or hexStr
end
print(hexStr)
print('#colors', math.ceil(#hexStr / 6))
print('#chars', #byteStr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment