Created
December 1, 2018 13:30
-
-
Save Egor-Skriptunoff/1236e0f475ab4cdf4f3511d893c254aa to your computer and use it in GitHub Desktop.
Convertor from GSM7 to ASCII
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Convertor from GSM7 to ASCII | |
| -- Usage: | |
| -- print(GSM7_to_ASCII("F4F29C9E769F1B")) | |
| function GSM7_to_ASCII(hex_string) | |
| local GSM_Base = { | |
| [0] = '@', '£', '$', '¥', 'è', 'é', 'ù', 'ì', 'ò', 'Ç', '\n', | |
| 'Ø', 'ø', '\r', 'Å', 'å', 'Δ', '_', 'Φ', 'Γ', 'Λ', 'Ω', 'Π', | |
| 'Ψ', 'Σ', 'Θ', 'Ξ', '', 'Æ', 'æ', 'ß', 'É', [36] = '¤', | |
| [64] = '¡', [91] = 'Ä', [92] = 'Ö', [93] = 'Ñ', [94] = 'Ü', | |
| [95] = '§', [96] = '¿', [123] = 'ä', [124] = 'ö', [125] = 'ñ', | |
| [126] = 'ü', [127] = 'à' | |
| } | |
| local GSM_Ext = { | |
| [20] = '^', [40] = '{', [41] = '}', [47] = '\\', [60] = '[', | |
| [61] = '~', [62] = ']', [64] = '|', [101] = '€' | |
| } | |
| local buffer = 0 | |
| local buffer_width = 0 | |
| local esc = false | |
| local result = {} | |
| for hh in hex_string:gmatch"%x%x" do | |
| buffer = buffer + 2^buffer_width * tonumber(hh, 16) | |
| buffer_width = buffer_width + 8 | |
| repeat | |
| local c = buffer % 128 | |
| buffer = (buffer - c) / 128 | |
| buffer_width = buffer_width - 7 | |
| if c == 27 then | |
| esc = true | |
| else | |
| local symbol = esc and GSM_Ext[c] or GSM_Base[c] or string.char(c) | |
| esc = false | |
| table.insert(result, symbol) | |
| end | |
| until buffer_width < 7 | |
| end | |
| if buffer_width == 0 and result[#result] == "\r" then | |
| table.remove(result) -- remove padding | |
| end | |
| return table.concat(result) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment