AT指令收发短信,需要短信猫的支持
/// <summary>
/// 针对国内短信编码(USC2)
/// </summary>
public class USC2
{
public readonly static int MAX_CHAR_COUNT = 70;//最长可发送汉字个数
/**/
/// <summary>
/// 奇偶互换并补F
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static string ParityChange(string value)
{
string result = string.Empty;
int length = value.Length;
for (int i = 1; i < length; i += 2)//奇偶互换
{
result += value[i];
result += value[i - 1];
}
if (!(length % 2 == 0))//不是偶数则加上F,与最后一位互换
{
result += 'F';
result += value[length - 1];
}
return result;
}
/**/
/// <summary>
/// 短信内容编码
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <remarks>
/// 采用Big-Endian 字节顺序的 Unicode 格式编码,将高低位互换
/// 将转换后的短信内容存进字节数组
/// 去掉在进行Unicode格式编码中,两个字节中的"-",例如:00-21,变成0021
/// 将整条短信内容的长度除2,保留两位16进制数
/// </remarks>
public static string Encoding(string value)
{
Encoding encoding = System.Text.Encoding.BigEndianUnicode;
string result = string.Empty;
byte[] bytes = encoding.GetBytes(value);
for (int i = 0; i < bytes.Length; i++)
{
result += BitConverter.ToString(bytes, i, 1);
}
return result;
}
public static string EncodeingAddLength(string value)
{
string result = Encoding(value);
return String.Format("{0:X2}{1}", result.Length / 2, result);
}
/**/
/// <summary>
/// 短信中心号码编码
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string DecodingCenter(string phone)
{
string result = string.Empty;
result = ParityChange(phone);
result = String.Format("91{0}", result);//加上91
result = String.Format("{0:X2}{1}", result.Length / 2, result);
return result;
}