【问题标题】:Decode 7-bit GSM in Lua在 Lua 中解码 7 位 GSM
【发布时间】:2018-12-01 11:34:04
【问题描述】:

如何再次解码 7 位 GSM 字符(将其反转回 ascii)?

例子:

string_coded_7bit_GSM = "737AD80D9AB16E3510394C0F8362B1562CD692C1623910ECA6A3C174B31B"

decoded_string_ascii = "stan 3,75 data 11-11-2019 07:40:37"

【问题讨论】:

    标签: lua encode gsm


    【解决方案1】:
    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
    

    【讨论】:

    • @lhf - 来自维基百科页面。有关填充的信息是从在线 GSM-ASCII 编码器/解码器中获得的。
    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多