短消息发送7Bit AscII编码符号的问题

来源:互联网 发布:mac怎么打开身份不明 编辑:程序博客网 时间:2024/05/16 16:56

短消息发送7Bit AscII编码符号的问题

 

在以前写的一个通过GSM模块发送与接收短消息的业务系统已经运行很久了。前两天检查接收内容时发现有接收的内容中有乱码。与发送者联系了,了解他发送的是一些字母中间有下划线。因为接收纯字母已经做过测试了。我自己编辑了一条短消息“_”发上去,发现收到的也是乱码,再发别的AscII字母,完全正常。再发看来问题出在短消息对符号的解码上。

仔细检查了一遍解码程序,没有发现问题。然后又在网上找了一段别人的解码代码,来进行测试,居然发现解码后的符号也是乱码。打开数据库将原始接收的Byte字符串找出来,看到接收到的内容是“11”,十进制为17,是不可见字符,而“_”的AscII码是95。又将“_”附近的几个字符试了一下,居然值都与AscII码值不相符。

只好Google了,搜了一下“7 bit ”,找到了一个文件”The 7 bit default alphabet”,打开一看,晕,居然有个GSM 03.38规定了1277 Bit字符,与标准的ISO-8859-1不相符。

只好重新写一个对字符映射表了。本来想用一个数组或Hashtable来做映射,但是有几个字符居然点用两个Byte。牺牲点内存与CPU算了,在内存里建了个数据表,将映射表做到DataTable里了,然后用DataViewRowFilter来查找。可慢了一点,但用起来方便。

源码如下:

    public class Gsm7BitEncoding
    {
        DataTable dt7BitAlphabet = new DataTable();
        DataView dv;
        private static Gsm7BitEncoding _gsm7bit = new Gsm7BitEncoding();
        public static Gsm7BitEncoding GetInstance()
        {
            return _gsm7bit;
        }

        public Gsm7BitEncoding()
        {
            //添加列
            DataColumn dc=new DataColumn("I_Pre",typeof(byte));
            dc.DefaultValue=0;
            dt7BitAlphabet.Columns.Add(dc);


            dc = new DataColumn("I_GsmChar", typeof(byte));
            dc.DefaultValue = 0;
            dt7BitAlphabet.Columns.Add(dc);


            dc = new DataColumn("I_AscIIChar", typeof(byte));
            dc.DefaultValue = 0;
            dt7BitAlphabet.Columns.Add(dc);
           
#region 添加数据行
          dt7BitAlphabet.Rows.Add(new object[] { 0,0 , 64   });               
            dt7BitAlphabet.Rows.Add(new object[] { 0,1 , 163  });    
            dt7BitAlphabet.Rows.Add(new object[] { 0,2 , 36   });    
            dt7BitAlphabet.Rows.Add(new object[] { 0,3 , 165  });    
            dt7BitAlphabet.Rows.Add(new object[] { 0,4 , 232  });    
            dt7BitAlphabet.Rows.Add(new object[] { 0,5 , 233  });    
            dt7BitAlphabet.Rows.Add(new object[] { 0,6 , 249  });    
            dt7BitAlphabet.Rows.Add(new object[] { 0,7 , 236  });    
            dt7BitAlphabet.Rows.Add(new object[] { 0,8 , 242  });
            dt7BitAlphabet.Rows.Add(new object[] { 0, 9, 199 });    
            dt7BitAlphabet.Rows.Add(new object[] { 0,10 , 10   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,11 , 216  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,12 , 248  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,13 , 13   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,14 , 197  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,15 , 229  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,16 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,17 , 95   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,18 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,19 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,20 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,21 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,22 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,23 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,24 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,25 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,26 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,27 , 0    });  
            dt7BitAlphabet.Rows.Add(new object[] {27,10 , 12   }); 
            dt7BitAlphabet.Rows.Add(new object[] {27,20 , 94   }); 
            dt7BitAlphabet.Rows.Add(new object[] {27,40 , 123   });
            dt7BitAlphabet.Rows.Add(new object[] {27,41 , 125   });
            dt7BitAlphabet.Rows.Add(new object[] {27,47 , 92   }); 
            dt7BitAlphabet.Rows.Add(new object[] {27,60 , 91   }); 
            dt7BitAlphabet.Rows.Add(new object[] {27,61 , 126   });
            dt7BitAlphabet.Rows.Add(new object[] {27,62 , 93   }); 
            dt7BitAlphabet.Rows.Add(new object[] {27,64 , 124   });
            dt7BitAlphabet.Rows.Add(new object[] {27,101 , 164   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,28 , 198  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,29 , 230  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,30 , 223  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,31 , 201  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,32 , 32   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,33 , 33   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,34 , 34   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,35 , 35   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,36 , 164  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,37 , 37   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,38 , 38   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,39 , 39   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,40 , 40   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,41 , 41   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,42 , 42   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,43 , 43   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,44 , 44   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,45 , 45   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,46 , 46   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,47 , 47   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,48 , 48   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,49 , 49   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,50 , 50   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,51 , 51   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,52 , 52   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,53 , 53   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,54 , 54   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,55 , 55   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,56 , 56   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,57 , 57   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,58 , 58   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,59 , 59   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,60 , 60   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,61 , 61   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,62 , 62   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,63 , 63   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,64 , 161  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,65 , 65   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,66 , 66   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,67 , 67   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,68 , 68   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,69 , 69   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,70 , 70   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,71 , 71   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,72 , 72   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,73 , 73   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,74 , 74   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,75 , 75   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,76 , 76   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,77 , 77   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,78 , 78   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,79 , 79   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,80 , 80   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,81 , 81   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,82 , 82   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,83 , 83   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,84 , 84   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,85 , 85   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,86 , 86   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,87 , 87   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,88 , 88   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,89 , 89   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,90 , 90   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,91 , 196  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,92 , 214  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,93 , 209  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,94 , 220  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,95 , 167  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,96 , 191  });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,97 , 97   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,98 , 98   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,99 , 99   });  
            dt7BitAlphabet.Rows.Add(new object[] { 0,100 , 100   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,101 , 101   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,102 , 102   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,103 , 103   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,104 , 104   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,105 , 105   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,106 , 106   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,107 , 107   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,108 , 108   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,109 , 109   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,110 , 110   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,111 , 111   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,112 , 112   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,113 , 113   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,114 , 114   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,115 , 115   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,116 , 116   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,117 , 117   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,118 , 118   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,119 , 119   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,120 , 120   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,121 , 121   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,122 , 122   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,123 , 228   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,124,124   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,125,125   });
            dt7BitAlphabet.Rows.Add(new object[] { 0,126,126   });
#endregion

            dv = dt7BitAlphabet.DefaultView;
        }
        /// <summary>
        /// 获取AscII字符串的Gsm7Bit字符编码的长度
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public int GetByteCount(string s)
        {
            byte[] gsmBytes = GetBytes(s);

            return gsmBytes.Length;
        }
        /// <summary>
        /// 将AscII字符串转换为Gsm7Bit字符数据
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public byte[] GetBytes(string s)
        {
            byte[] ascBts;
            List<byte> gsmBts=new List<byte>();
            ascBts = ASCIIEncoding.ASCII.GetBytes(s);
            for (int i = 0; i < ascBts.Length; i++)
            {
               dv.RowFilter = "I_AscIIChar=" + ascBts[i].ToString();
                if (dv.Count>0)
                {
                    byte pre = (byte)dv[0][0];
                    byte gsmchar = (byte)dv[0][1];
                   
                    if (pre==27)
                    {
                        gsmBts.Add(27);
                    }
                    gsmBts.Add(gsmchar);
                }
            }
            return gsmBts.ToArray();
        }
        /// <summary>
        /// 从GSM7Bit字符数据转换为AscII字符串
        /// </summary>
        /// <param name="gsmBytes"></param>
        /// <returns></returns>
        public string GetString(byte[] gsmBytes)
        {
            StringBuilder sb = new StringBuilder();
            string condition = "";
            for (int i = 0; i < gsmBytes.Length; i++)
            {
                if (gsmBytes[i] == 27)
                {
                    condition = " I_Pre=27 and ";
                    i++;
                    condition += " I_GsmChar=" + gsmBytes[i].ToString();
                }
                else
                {
                    condition = "I_Pre=0 and I_GsmChar=" + gsmBytes[i].ToString();
                }
                dv.RowFilter = condition;

                if (dv.Count>0)
                {
                    System.Diagnostics.Debug.Assert((byte)dv[0][2] != 0);
                    sb.Append((char)(byte)dv[0][2]);
                }
            }
            return sb.ToString();
        }
    }

原创粉丝点击