【问题标题】:ASCII to TBCD Conversion in CC 中的 ASCII 到 TBCD 转换
【发布时间】:2013-01-03 06:48:24
【问题描述】:

我想在 C 中将 ASCII string 转换为 TBCD(Telephony Binary-Coded Decimal) 格式,反之亦然。我搜索了很多网站,但找不到答案。

【问题讨论】:

    标签: c ascii tbcd


    【解决方案1】:

    最简单的方法可能是使用一对数组将每个 ASCII 字符映射到其对应的 TBCD 字符。反之亦然。

    来自what I read on Wikipedia,您应该使用以下内容:

    const char *tbcd_to_ascii = "0123456789*#abc";
    const char ascii_to_tbcd[] = {
     15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* filler when there is an odd number of digits */
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0,11, 0, 0, 0, 0, 0, 0,10, 0, 0, 0, 0, 0, /* # * */
      0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0 /* digits */
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0,12,13,14            /* a b c */
    };
    

    如果您有 TBCD,您可以将其转换为 ASCII:

    /* The TBCD to convert */
    int tbcd[] = { 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe };
    /* The converted ASCII string will be stored here. Make sure to have
       enough room for the result and a terminating 0 */
    char ascii[16] = { 0 };
    /* Convert the TBCD to ASCII */
    int i;
    for (i = 0; i < sizeof(tbcd)/sizeof(*tbcd); i++) {
        ascii[2 * i] = tbcd_to_ascii[tbcd[i] & 0x0f];
        ascii[2 * i + 1] = tbcd_to_ascii[(tbcd[i] & 0xf0) >> 4];
    }
    

    从 ASCII 转换为 TBCD:

    /* ASCII number */
    const char *ascii = "0123456789*#abc";
    /* The converted TBCD number will be stored here. Make sure to have enough room for the result */
    int tbcd[8];
    int i;
    int len = strlen(ascii);
    for (i = 0; i < len; i += 2)
        tbcd[i / 2] = ascii_to_tbcd[ascii[i]]
            | (ascii_to_tbcd[ascii[i + 1]] << 4);
    

    编辑:@Kevin 指出 TBCD 每个字节包含 两个 位。

    【讨论】:

    • 这里给出了 BCD 的 C 实现:en.wikipedia.org/wiki/Double_dabble
    • @kmkaplan 假设我有一个 TBCD 格式的 IMSI 可以转换为 ASCII。上面的数组有什么帮助?
    • @DJ 我真的不知道 IMSI 是什么,但我编辑了我的答案以举例说明如何将 TBCD 数组转换为 ASCII 字符串。
    • @kmkaplan 您的回答对我很有帮助。谢谢 :) IMSI 是附加到我们手机的每个 SIM 卡上的国际移动用户身份。
    • TBCD 将两个数字放在一个字节中。除非我遗漏了什么,否则这个答案不会这样做并且是错误的。
    【解决方案2】:
    #include <ctype.h>
    int cnv_tbcd(char *str, char *tbcd) {
      int c = 0;
      int err = 0;
      for (c=0; str[c]; c++) {
        if (isdigit(str[c])) {
          tbcd[c] = str[c] & 0x0f;
        } else {
          switch(str[c]) {
            case '*': tbcd[c]  = 0x0a; break;
            case '#': tbcd[c]  = 0x0b; break;
            case 'a': tbcd[c]  = 0x0c; break;
            case 'b': tbcd[c]  = 0x0d; break;
            case 'c': tbcd[c]  = 0x0e; break;
            default:  tbcd[c] = 0xff; err++;
          }   
        }   
      }
      if (c % 2 == 0) {
        tbcd[c]   = 0x0f;
        tbcd[c+1] = 0;
      }
      return err;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-31
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多