2012-10-29 46 views
5

我發現這個post關於如何將ascii數據編碼爲7位GSM字符集,我將如何再解碼7位GSM字符(將其反轉回ascii)?解碼7位GSM

+0

做相反的...... –

+0

你卡在哪裏? – monkut

回答

3

對於Python2:

import binascii 
gsm = ("@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./:;<=>?" 
     "¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ`¿abcdefghijklmnopqrstuvwxyzäöñüà") 
ext = ("````````````````````^```````````````````{}`````\\````````````[~]`" 
     "|````````````````````````````````````€``````````````````````````") 

def gsm_encode(plaintext): 
    result = [] 
    for c in plaintext: 
     idx = gsm.find(c) 
     if idx != -1: 
      result.append(chr(idx)) 
      continue 
     idx = ext.find(c) 
     if idx != -1: 
      result.append(chr(27) + chr(idx)) 
    return ''.join(result).encode('hex') 

def gsm_decode(hexstr): 
    res = hexstr.decode('hex') 
    res = iter(res) 
    result = [] 
    for c in res: 
     if c == chr(27): 
      c = next(res) 
      result.append(ext[ord(c)]) 
     else: 
      result.append(gsm[ord(c)]) 
    return ''.join(result) 

code = gsm_encode("Hello World {}") 
print(code) 
# 64868d8d903a7390938d853a1b281b29 
print(gsm_decode(code)) 
# Hello World {} 
2

有一個很簡單的解決辦法:

轉換以二進制八位位組的十六進制將每個八位位組的陣列,但以相反的順序(整個八位位組,而不是比特)因爲這是他們發送的方式。閱讀從右字符串中的7個組餘下的數目是在GSM 7位表

例如字符代碼:

C7F7FBCC2E03代表「谷歌」

以相反的順序串

03-2E-CC-FB-F7-C7

的六個八位字節是

00000011-00101110-11001100-11111011-11110111-1 1000111

的七位是

000000-1100101-1101100-1100111-1101111-1101111-1000111

由右至左讀則是:

七重峯小數勇氣雙字符在GSM 7位表

1000111-71-G

1101111-111鄰

1101111-111鄰

1100111-103-G

1101100-108 -1-

1100101-101-E

丟棄最後0000000值

5

例如:

C7F7FBCC2E03代表'Google'
Python 3.4

def gsm7bitdecode(f): 
    f = ''.join(["{0:08b}".format(int(f[i:i+2], 16)) for i in range(0, len(f), 2)][::-1]) 
    return ''.join([chr(int(f[::-1][i:i+7][::-1], 2)) for i in range(0, len(f), 7)]) 

打印(gsm7bitdecode( 'C7F7FBCC2E03'))

谷歌

+1

也適用於Python 2.7.11 –

0

我用C寫such decoder爲OpenWrt的設備:

uint8_t get_data (char input, uint8_t * output) 
{ 
    if (input - '0' >= 0 && '9' - input >= 0) { 
     * output = input - '0'; 
    } else if (input - 'a' >= 0 && 'f' - input >= 0) { 
     * output = input - 'a' + 10; 
    } else if (input - 'A' >= 0 && 'F' - input >= 0) { 
     * output = input - 'A' + 10; 
    } else { 
     return 1; 
    } 
    return 0; 
} 

uint8_t get_data_pair (const char * input, uint8_t * output) 
{ 
    uint8_t data; 
    if (get_data (* input, &data) != 0) { 
     return 1; 
    } 
    * output = data << 4; 
    if (get_data (* (input + 1), &data) != 0) { 
     return 2; 
    } 
    * output = * output | data; 
    return 0; 
} 

int main (int argc, char * argv []) 
{ 
    if (argc != 2) { 
     fputs ("required argument: hex\n", stderr); 
     return 1; 
    } 

    char * hex = argv[1]; 
    uint16_t data = 0; 
    uint8_t data_length = 0; 

    while (*hex != '\0') { 
     uint8_t new_data; 
     if (get_data_pair (hex, &new_data) != 0) { 
      fprintf (stderr, "invalid hex: bad pair %.2s\n", hex); 
      putchar ('\n'); 
      return 2; 
     } 
     hex += 2; 

     data = new_data << data_length | data; 
     data_length += 8; 

     while (data_length >= 7) { 
      putchar (data & 0x7f); 
      data = data >> 7; 
      data_length -= 7; 
     } 
    } 

    putchar ('\n'); 
    return 0; 
}