/// <summary> /// 将7-Bit编码字节数换算成UCS2编码字符数 /// </summary> /// <param name="source">字符串</param> /// <param name="index">起始索引号</param> /// <param name="septets">要换算的7-Bit编码字节数</param> /// <returns>UCS2编码字符数</returns> private static Int32 SeptetsToChars(String source, Int32 index, Int32 septets) { if (String.IsNullOrEmpty(source)) { return(0); } Int32 Count = 0; Int32 i = index; for (; i < source.Length; i++) { UInt16 Code = Convert.ToUInt16(source[i]); if (UCS2ToBIT7.ContainsKey(Code) && UCS2ToBIT7[Code] > 0xFF) { Count++; } if (++Count >= septets) { if (Count == septets) { i++; } break; } } return(i - index); }
/// <summary> /// 判断字符串是否在GSM缺省字符集内 /// </summary> /// <param name="source">要评估的字符串</param> /// <returns> /// true:在GSM缺省字符集内,可以使用7-Bit编码 /// false:不在GSM缺省字符集内,只能使用UCS2编码 /// </returns> private static Boolean isGSMString(String source) { if (String.IsNullOrEmpty(source)) { return(true); } foreach (Char Letter in source) { UInt16 Code = Convert.ToUInt16(Letter); if (!(isBIT7Same(Code) || UCS2ToBIT7.ContainsKey(Code))) { return(false); } } return(true); }
/// <summary> /// 将UCS2编码字符串转换成7-Bit编码字节 序列 /// </summary> /// <param name="UDC">用户数据内容</param> /// <param name="Septets">7-Bit编码字符数</param> /// <returns>7-Bit编码字节序列</returns> private static Byte[] BIT7Encoding(String UDC, out Int32 Septets) { Byte[] Bit7Array = new Byte[UDC.Length << 1]; Septets = 0; foreach (Char Letter in UDC) { UInt16 Code = Convert.ToUInt16(Letter); if (isBIT7Same(Code)) { // 编码不变 Bit7Array[Septets++] = Convert.ToByte(Code); } else { if (UCS2ToBIT7.ContainsKey(Code)) { UInt16 Transcode = UCS2ToBIT7[Code]; // 转换码 if (Transcode > 0xFF) { // 转义序列 Bit7Array[Septets++] = Convert.ToByte(Transcode >> 8); Bit7Array[Septets++] = Convert.ToByte(Transcode & 0xFF); } else { Bit7Array[Septets++] = Convert.ToByte(Transcode); } } else { // 未知字符 Bit7Array[Septets++] = Convert.ToByte('?'); } } } // 重新调整大小 Array.Resize(ref Bit7Array, Septets); return(Bit7Array); }
/// <summary> /// 计算字符串需要的7-Bit编码字节数 /// </summary> /// <param name="source">字符串</param> /// <returns>7-Bit编码字节数</returns> private static Int32 SeptetsLength(String source) { if (String.IsNullOrEmpty(source)) { return(0); } Int32 Length = source.Length; foreach (Char Letter in source) { UInt16 Code = Convert.ToUInt16(Letter); if (UCS2ToBIT7.ContainsKey(Code)) { if (UCS2ToBIT7[Code] > 0xFF) { Length++; } } } return(Length); }