在 Lua 中解码 7 位 GSM
Decode 7-bit GSM in Lua
如何再次解码 7 位 GSM 字符(将其反转回 ascii)?
示例:
string_coded_7bit_GSM = "737AD80D9AB16E3510394C0F8362B1562CD692C1623910ECA6A3C174B31B"
decoded_string_ascii = "stan 3,75 data 11-11-2019 07:40:37"
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
用法:
local string_coded_7bit_GSM = "737AD80D9AB16E3510394C0F8362B1562CD692C1623910ECA6A3C174B31B"
local decoded_string_ascii = GSM7_to_ASCII(string_coded_7bit_GSM)
print(decoded_string_ascii) --> stan 3,75 data 11-11-2019 07:40:37
如何再次解码 7 位 GSM 字符(将其反转回 ascii)?
示例:
string_coded_7bit_GSM = "737AD80D9AB16E3510394C0F8362B1562CD692C1623910ECA6A3C174B31B"
decoded_string_ascii = "stan 3,75 data 11-11-2019 07:40:37"
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
用法:
local string_coded_7bit_GSM = "737AD80D9AB16E3510394C0F8362B1562CD692C1623910ECA6A3C174B31B"
local decoded_string_ascii = GSM7_to_ASCII(string_coded_7bit_GSM)
print(decoded_string_ascii) --> stan 3,75 data 11-11-2019 07:40:37