短信猫操作类一,会计中阿拉伯数字变数字繁体大写

By admin in 4858.com on 2019年4月13日

//注释:本文非原来的著作是透过网上的代码修改大概10%而成的,忘了出处了,所以最近不申明出处

//注释:本文非原来的文章是通过网上的代码修改大约一成而成的,忘了出处了,所以临时不申明出处

会计师中阿拉伯数字变数字繁体大写,阿拉伯题写

//注释:本文非原来的文章是通过网上的代码修改大概10%而成的,忘了出处了,所以一时不注脚出处

//转化的工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IntegrationSystemChemicalIndustry.Product
{
/// <summary>
  /// 阿拉伯数字转粤语数字,中文数字转阿拉伯数字。
  /// by hcling97.Net 2007.03
  /// </summary>
public class NumberConventer
{
private string[] ArabinNum = { “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”,
“8”, “9” };
private string[] ChnNum = { “零”, “壹”, “贰”, “叁”, “肆”, “伍”, “陆”,
“柒”, “捌”, “玖”, “拾”, “佰”, “仟”, “万”, “亿” };

private string[] Union = { “”, “拾”, “佰”, “仟” };
public NumberConventer()
{
//
// TODO: 在此地添加构造函数逻辑
//
}
public string ArabToChn(Decimal ArabNum, out string msg)
{
string neg = string.Empty;
neg = (ArabNum < 0) ? “负” : “”;
string result = string.Empty;
string[] part =
(ArabNum.ToString().Replace(“-“, string.Empty)).Split(‘.’);
Int64 temp = Convert.ToInt64(part[0]);
Int64 epart = temp;
string dotpart =
(part.Length > 1) ? part[1] : string.Empty;
if (part.Length > 1)
{
dotpart = this.GetDotPart(dotpart);
}
string tmp = string.Empty;
string lasttemp = string.Empty;
for (int i = 0; i <= ((epart.ToString().Length – 1) / 4); i++)
{
int thousand = 0;
thousand = Convert.ToInt32(temp %
10000);
temp = temp / 10000;
lasttemp = tmp;
tmp = this.GetThousandPart(thousand);

if (i == 0)
{
result = tmp;
lasttemp = tmp;
}
if (i == 1)//重返的是万
{
if (result == “零”)
{
result = string.Empty;
}
result = tmp + “万” +
((lasttemp.IndexOf(“千”) == -1 && lasttemp != “零”) ? “零” : “”) +
result;
}
if (i == 2)//亿
{
if (result.IndexOf(“零万”) != -1)
{

result = result.Replace(“零万”, string
.Empty);
}
result = tmp + “亿” + ((lasttemp.IndexOf(“千”) == -1 && lasttemp !=
“零”) ? “零” : “”) + result;
}
if (i == 3)//万亿
{
if (result.IndexOf(“零亿”) != -1)
{
result = result.Replace(“零亿”, “亿”);
}
result = tmp + “万” + ((lasttemp.IndexOf(“千”) == -1 && lasttemp !=
“零”) ? “零” : “”) + result;
}
if (i == 4)//亿亿
{
if (result.IndexOf(“零万”) != -1)
{

result = result.Replace(“零万”, string
.Empty);
}
result = tmp + “亿” + ((lasttemp.IndexOf(“千”) == -1 && lasttemp !=
“零”) ? “零” : “”) + result;
}
}
result = neg + result + dotpart;
msg = “成功转移!”;
return result;
}
/// <summary>
/// 处理小数部分
/// </summary>
/// <param name=”dotPart”></param>
/// <returns></returns>
private string GetDotPart(string dotPart)
{
string result = “点”;
for (int i = 0; i < dotPart.Length; i++)
{
result += ChnNum[Convert.ToInt32(dotPart[i].ToString())];
}
for (int j = 0; j < result.Length; j++)
//去除无效零或点
{

if (result[result.Length – j – 1].ToString() != “点” &&
result[result.Length – j – 1].ToString() != “零”)
{
break;
}
else
{
result =result.Substring(0, (result.Length – j – 1));
}
}
return result;
}
/// <summary>
    /// 万位以下的分析
    /// </summary>
    /// <returns></returns>
private string GetThousandPart(int number)
{
if (number == 0)
{
return “零”;
}
string result = string.Empty;
bool lowZero = false;
//记录低位有未有找到非零值,没找到置true
bool befZero = false;
//记录前壹个人是否非零值,是0则置true
int temp = number;
int index = number.ToString().Length;
for (int i = 0; i < index; i++)
{
int n = temp % 10;
temp = temp / 10;
if (i == 0) //起始位
{
if (n == 0)
{
lowZero = true; //低位有0
befZero = true; //前位为0
}
else
{
result = ChnNum[n];
}
}
else
{
if (n != 0)
{
result = ChnNum[n] + Union[i] + result;
lowZero = false;
befZero = false;
}
else
{
if (!lowZero)
{
if (!befZero)
//低位有数,且前位不为0,本位为0填零
//eg.5906
{
result = ChnNum[n] + result;
befZero = true;
}
else
//低位有数,且前位为0省略零eg. 500捌
{
}
}
else //低位为0
{
if (!befZero)//理论上不存在eg 5080
{
result = ChnNum[n] + result;
befZero = true;
}
else //eg. 5000
{
}
}
}
}
}
return result;
}
public Decimal ChnToArab(string ChnNum)
{
Decimal result = 0;
string temp = ChnNum;
bool neg = false;
if (ChnNum.IndexOf(“负”) != -1)
{
neg = true;
temp = temp.Replace(“负”, string.Empty);
}
string pre = string.Empty;
string abo = string.Empty;
temp = temp.Replace(“点”, “.”);
string[] part = temp.Split(‘.’);
pre = part[0];
Decimal dotPart = 0;
if (part.Length > 1)
{
abo = part[1];
dotPart = this.GetArabDotPart(abo);
}

int yCount = 0;
//”亿”的个数,有不小大概出现亿亿。
//int yPos = 0;

int index = 0;
while (index < pre.Length)
{
if (pre.IndexOf(“亿”, index) != -1)
{
yCount++;
//yPos = pre.IndexOf(“亿”,index);
index = pre.IndexOf(“亿”, index) + 1;
}
else
{
break;
}
}
if (yCount == 2)//亿亿
{
pre = pre.Replace(“亿”, “,”);
string[] sp = pre.Split(‘,’);
result =
(neg ? -1 : 1) * ((this.HandlePart(sp[0]) * 10000000000000000) +
(this.HandlePart(sp[1]) * 100000000) + this.HandlePart(sp[2])) +
dotPart;
}
else
{
if (yCount == 1)
{
pre = pre.Replace(“亿”, “,”);
string[] sp = pre.Split(‘,’);
result =(neg ? -1 : 1) * ((this.HandlePart(sp[0]) * 100000000) +
this.HandlePart(sp[1])) + dotPart;
}
else
{
if (yCount == 0)
{
result =(neg ? -1 : 1) * this.HandlePart(pre) + dotPart;
}
}
}
return result;

}
/// <summary>
    /// 处理亿以下内容。
    /// </summary>
    /// <param name=”num”></param>
    /// <returns></returns>
private Decimal HandlePart(string num)
{
Decimal result = 0;
string temp = num;
temp = temp.Replace(“万”, “,”);
string[] part = temp.Split(‘,’);
for (int i = 0; i < part.Length; i++)
{
result +=Convert.ToDecimal(this.GetArabThousandPart(part[part.Length –
i – 1])) * Convert.ToDecimal((System.Math.Pow(10000,
Convert.ToDouble(i))));
}
return result;
}

/// <summary>
    /// 取得阿拉伯数字小数部分。
    /// </summary>
    /// <returns></returns>
private Decimal GetArabDotPart(string dotpart)
{
Decimal result = 0.00M;
string spe = “0.”;
for (int i = 0; i < dotpart.Length; i++)
{
spe +=this.switchNum(dotpart[i].ToString()).ToString();
}
result = Convert.ToDecimal(spe);
return result;
}

public int GetArabThousandPart(string number)
{

string ChnNumString = number;
if (ChnNumString == “零”)
{
return 0;
}
if (ChnNumString != string.Empty)
{
if (ChnNumString[0].ToString() == “十”)
{
ChnNumString = “一” + ChnNumString;
}
}

ChnNumString = ChnNumString.Replace(“零”, string.Empty);
//去除全部的零
int result = 0;
int index = ChnNumString.IndexOf(“千”);
if (index != -1)
{
result +=this.switchNum(ChnNumString.Substring(0, index)) * 1000;
ChnNumString =ChnNumString.Remove(0, index + 1);
}
index = ChnNumString.IndexOf(“百”);
if (index != -1)
{
result +=this.switchNum(ChnNumString.Substring(0 , index)) * 100;
ChnNumString = ChnNumString.Remove(0, index + 1);
}
index = ChnNumString.IndexOf(“十”);
if (index != -1)
{
result += this.switchNum(ChnNumString.Substring(0 , index)) * 10;
ChnNumString = ChnNumString.Remove(0, index + 1);
}
if (ChnNumString != string.Empty)
{
result += this.switchNum(ChnNumString);
}
return result;
}
/// <summary>
    /// 取得汉字对应的阿拉伯数字
    /// </summary>
    /// <param name=”n”></param>
    /// <returns></returns>
private int switchNum(string n)
{
switch (n)
{
case “零”:
{
return 0;
}
case “一”:
{
return 1;
}
case “二”:
{
return 2;
}
case “三”:
{
return 3;
}
case “四”:
{
return 4;
}
case “五”:
{
return 5;
}
case “六”:
{
return 6;
}
case “七”:
{
return 7;
}
case “八”:
{
return 8;
}
case “九”:
{
return 9;
}
default:
{
return -1;
}
}
}
}
}

 

///页面调用的方法

NumberConventer NC = new NumberConventer();//新建工作类对象
string TP = string.Empty;//TP转换结果
this.txtTotalPriceUP.Text =
NC.ArabToChn(Convert.ToDecimal(_Total普赖斯), out
TP);//那些格局重返结果,类型string,能处理的数字大小为一亿个亿

 

//注释:本文非原作是通过网上的代码修改大概十分一而成的,忘了出处了,所以一时半刻不表明出…

一个C#能源分享平台,专业分享学习高品质代码,每一周期铺排学习职分,激发学习C#兴趣!(QQ群:128874886) 

//转化的工具类

//转化的工具类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

关于C#操作短信猫的小说在天涯论坛也有为数不少,在那之中个人认为比较规范的正是给自身1杯酒
的短信猫软件达成的几种博客了!

namespace IntegrationSystemChemicalIndustry.Product
{
/// <summary>
  /// 阿拉伯数字转粤语数字,普通话数字转阿拉伯数字。
  /// by hcling97.Net 2007.03
  /// </summary>
public class NumberConventer
{
private string[] ArabinNum = { “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”,
“8”, “9” };
private string[] ChnNum = { “零”, “壹”, “贰”, “叁”, “肆”, “伍”, “陆”,
“柒”, “捌”, “玖”, “拾”, “佰”, “仟”, “万”, “亿” };

namespace IntegrationSystemChemicalIndustry.Product
{
/// <summary>
  /// 阿拉伯数字转汉语数字,普通话数字转阿拉伯数字。
  /// by hcling97.Net 2007.03
  /// </summary>
public class NumberConventer
{
private string[] ArabinNum = { “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”,
“8”, “9” };
private string[] ChnNum = { “零”, “壹”, “贰”, “叁”, “肆”, “伍”, “陆”,
“柒”, “捌”, “玖”, “拾”, “佰”, “仟”, “万”, “亿” };

短信猫软件的实现(C#)<一>熟知串口

private string[] Union = { “”, “拾”, “佰”, “仟” };
public NumberConventer()
{
//
// TODO: 在那边添加构造函数逻辑
//
}
public string ArabToChn(Decimal ArabNum, out string msg)
{
string neg = string.Empty;
neg = (ArabNum < 0) ? “负” : “”;
string result = string.Empty;
短信猫操作类一,会计中阿拉伯数字变数字繁体大写。string[] part =
(ArabNum.ToString().Replace(“-“, string.Empty)).Split(‘.’);
Int64 temp = Convert.ToInt64(part[0]);
Int64 epart = temp;
string dotpart =
(part.Length > 1) ? part[1] : string.Empty;
if (part.Length > 1)
{
dotpart = this.GetDotPart(dotpart);
}
string tmp = string.Empty;
string lasttemp = string.Empty;
for (int i = 0; i <= ((epart.ToString().Length – 1) / 4); i++)
{
int thousand = 0;
thousand = Convert.ToInt32(temp %
10000);
temp = temp / 10000;
lasttemp = tmp;
tmp = this.GetThousandPart(thousand);

private string[] Union = { “”, “拾”, “佰”, “仟” };
public NumberConventer()
{
//
// TODO: 在此间添加构造函数逻辑
//
}
public string ArabToChn(Decimal ArabNum, out string msg)
{
string neg = string.Empty;
neg = (ArabNum < 0) ? “负” : “”;
string result = string.Empty;
string[] part =
(ArabNum.ToString().Replace(“-“, string.Empty)).Split(‘.’);
Int64 temp = Convert.ToInt64(part[0]);
Int64 epart = temp;
string dotpart =
(part.Length > 1) ? part[1] : string.Empty;
if (part.Length > 1)
{
dotpart = this.GetDotPart(dotpart);
}
string tmp = string.Empty;
string lasttemp = string.Empty;
for (int i = 0; i <= ((epart.ToString().Length – 1) / 4); i++)
{
int thousand = 0;
thousand = Convert.ToInt32(temp %
10000);
temp = temp / 10000;
lasttemp = tmp;
tmp = this.GetThousandPart(thousand);

短信猫软件的贯彻(C#)<二>
AT指令调节和测试器的大约完结

if (i == 0)
{
result = tmp;
lasttemp = tmp;
}
if (i == 1)//重返的是万
{
if (result == “零”)
{
result = string.Empty;
}
result = tmp + “万” +
((lasttemp.IndexOf(“千”) == -1 && lasttemp != “零”) ? “零” : “”) +
result;
}
if (i == 2)//亿
{
if (result.IndexOf(“零万”) != -1)
{

if (i == 0)
{
result = tmp;
lasttemp = tmp;
}
if (i == 壹)//再次来到的是万
{
if (result == “零”)
{
result = string.Empty;
}
result = tmp + “万” +
((lasttemp.IndexOf(“千”) == -1 && lasttemp != “零”) ? “零” : “”) +
result;
}
if (i == 2)//亿
{
if (result.IndexOf(“零万”) != -1)
{

短信猫软件的落到实处(C#)<3>PDU格式短信解析

result = result.Replace(“零万”, string
.Empty);
}
result = tmp + “亿” + ((lasttemp.IndexOf(“千”) == -1 && lasttemp !=
“零”) ? “零” : “”) + result;
}
if (i == 3)//万亿
{
if (result.IndexOf(“零亿”) != -1)
{
result = result.Replace(“零亿”, “亿”);
}
result = tmp + “万” + ((lasttemp.IndexOf(“千”) == -1 && lasttemp !=
“零”) ? “零” : “”) + result;
}
if (i == 4)//亿亿
{
if (result.IndexOf(“零万”) != -1)
{

result = result.Replace(“零万”, string
.Empty);
}
result = tmp + “亿” + ((lasttemp.IndexOf(“千”) == -1 && lasttemp !=
“零”) ? “零” : “”) + result;
}
if (i == 3)//万亿
{
if (result.IndexOf(“零亿”) != -1)
{
result = result.Replace(“零亿”, “亿”);
}
result = tmp + “万” + ((lasttemp.IndexOf(“千”) == -1 && lasttemp !=
“零”) ? “零” : “”) + result;
}
if (i == 4)//亿亿
{
if (result.IndexOf(“零万”) != -1)
{

短信猫软件的实现(C#)<四>PDU格式编码C#实现

result = result.Replace(“零万”, string
.Empty);
}
result = tmp + “亿” + ((lasttemp.IndexOf(“千”) == -1 && lasttemp !=
“零”) ? “零” : “”) + result;
}
}
result = neg + result + dotpart;
msg = “成功转移!”;
return result;
}
/// <summary>
/// 处理小数部分
/// </summary>
/// <param name=”dotPart”></param>
/// <returns></returns>
private string GetDotPart(string dotPart)
{
string result = “点”;
for (int i = 0; i < dotPart.Length; i++)
{
result += ChnNum[Convert.ToInt32(dotPart[i].ToString())];
}
for (int j = 0; j < result.Length; j++)
//去除无效零或点
{

result = result.Replace(“零万”, string
.Empty);
}
result = tmp + “亿” + ((lasttemp.IndexOf(“千”) == -1 && lasttemp !=
“零”) ? “零” : “”) + result;
}
}
result = neg + result + dotpart;
msg = “成功转移!”;
return result;
}
/// <summary>
/// 处理小数部分
/// </summary>
/// <param name=”dotPart”></param>
/// <returns></returns>
private string GetDotPart(string dotPart)
{
string result = “点”;
for (int i = 0; i < dotPart.Length; i++)
{
result += ChnNum[Convert.ToInt32(dotPart[i].ToString())];
}
for (int j = 0; j < result.Length; j++)
//去除无效零或点
{

短信猫软件的达成(C#)<5>PDU格式解码C#实现

if (result[result.Length – j – 1].ToString() != “点” &&
result[result.Length – j – 1].ToString() != “零”)
{
break;
}
else
{
result =result.Substring(0, (result.Length – j – 1));
}
}
return result;
}
/// <summary>
    /// 万位以下的辨析
    /// </summary>
    /// <returns></returns>
private string GetThousandPart(int number)
{
if (number == 0)
{
return “零”;
}
string result = string.Empty;
bool lowZero = false;
//记录低位有未有找到非零值,没找到置true
bool befZero = false;
//记录前一位是或不是非零值,是0则置true
int temp = number;
int index = number.ToString().Length;
for (int i = 0; i < index; i++)
{
int n = temp % 10;
temp = temp / 10;
if (i == 0) //起始位
{
if (n == 0)
{
lowZero = true; //低位有0
befZero = true; //前位为0
}
else
{
result = ChnNum[n];
}
}
else
{
if (n != 0)
{
result = ChnNum[n] + Union[i] + result;
lowZero = false;
befZero = false;
}
else
{
if (!lowZero)
{
if (!befZero)
//低位有数,且前位不为0,本位为0填零
//eg.5906
{
result = ChnNum[n] + result;
befZero = true;
}
else
//低位有数,且前位为0省略零eg. 500八
{
}
}
else //低位为0
{
if (!befZero)//理论上不设有eg 5080
{
result = ChnNum[n] + result;
befZero = true;
}
else //eg. 5000
{
}
}
}
}
}
return result;
}
public Decimal ChnToArab(string ChnNum)
{
Decimal result = 0;
string temp = ChnNum;
bool neg = false;
if (ChnNum.IndexOf(“负”) != -1)
{
neg = true;
temp = temp.Replace(“负”, string.Empty);
}
string pre = string.Empty;
string abo = string.Empty;
temp = temp.Replace(“点”, “.”);
string[] part = temp.Split(‘.’);
pre = part[0];
Decimal dotPart = 0;
if (part.Length > 1)
{
abo = part[1];
dotPart = this.GetArabDotPart(abo);
}

if (result[result.Length – j – 1].ToString() != “点” &&
result[result.Length – j – 1].ToString() != “零”)
{
break;
}
else
{
result =result.Substring(0, (result.Length – j – 1));
}
}
return result;
}
/// <summary>
    /// 万位以下的辨析
    /// </summary>
    /// <returns></returns>
private string GetThousandPart(int number)
{
if (number == 0)
{
return “零”;
}
string result = string.Empty;
bool lowZero = false;
//记录低位有未有找到非零值,没找到置true
bool befZero = false;
//记录前一个人是还是不是非零值,是0则置true
int temp = number;
int index = number.ToString().Length;
for (int i = 0; i < index; i++)
{
int n = temp % 10;
temp = temp / 10;
if (i == 0) //起始位
{
if (n == 0)
{
lowZero = true; //低位有0
befZero = true; //前位为0
}
else
{
result = ChnNum[n];
}
}
else
{
if (n != 0)
{
result = ChnNum[n] + Union[i] + result;
lowZero = false;
befZero = false;
}
else
{
if (!lowZero)
{
if (!befZero)
//低位有数,且前位不为0,本位为0填零
//eg.5906
{
result = ChnNum[n] + result;
befZero = true;
}
else
//低位有数,且前位为0省略零eg. 500八
{
}
}
else //低位为0
{
if (!befZero)//理论上不设有eg 5080
{
result = ChnNum[n] + result;
befZero = true;
}
else //eg. 5000
{
}
}
}
}
}
return result;
}
public Decimal ChnToArab(string ChnNum)
{
Decimal result = 0;
string temp = ChnNum;
bool neg = false;
if (ChnNum.IndexOf(“负”) != -1)
{
neg = true;
temp = temp.Replace(“负”, string.Empty);
}
string pre = string.Empty;
string abo = string.Empty;
temp = temp.Replace(“点”, “.”);
string[] part = temp.Split(‘.’);
pre = part[0];
Decimal dotPart = 0;
if (part.Length > 1)
{
abo = part[1];
dotPart = this.GetArabDotPart(abo);
}

短信猫软件的贯彻(C#)<陆>发送接收短信C#实现(API)

int yCount = 0;
//”亿”的个数,有希望现身亿亿。
//int yPos = 0;

int yCount = 0;
//”亿”的个数,有望出现亿亿。
//int yPos = 0;

短信猫软件的落到实处(C#)<七>短信猫(简化测试版)达成

int index = 0;
while (index < pre.Length)
{
if (pre.IndexOf(“亿”, index) != -1)
{
yCount++;
//yPos = pre.IndexOf(“亿”,index);
index = pre.IndexOf(“亿”, index) + 1;
}
else
{
break;
}
}
if (yCount == 2)//亿亿
{
pre = pre.Replace(“亿”, “,”);
string[] sp = pre.Split(‘,’);
result =
(neg ? -1 : 1) * ((this.HandlePart(sp[0]) * 10000000000000000) +
(this.HandlePart(sp[1]) * 100000000) + this.HandlePart(sp[2])) +
dotPart;
}
else
{
if (yCount == 1)
{
pre = pre.Replace(“亿”, “,”);
string[] sp = pre.Split(‘,’);
result =(neg ? -1 : 1) * ((this.HandlePart(sp[0]) * 100000000) +
this.HandlePart(sp[1])) + dotPart;
}
else
{
if (yCount == 0)
{
result =(neg ? -1 : 1) * this.HandlePart(pre) + dotPart;
}
}
}
return result;

int index = 0;
while (index < pre.Length)
{
if (pre.IndexOf(“亿”, index) != -1)
{
yCount++;
//yPos = pre.IndexOf(“亿”,index);
index = pre.IndexOf(“亿”, index) + 1;
}
else
{
break;
}
}
if (yCount == 2)//亿亿
{
pre = pre.Replace(“亿”, “,”);
string[] sp = pre.Split(‘,’);
result =
(neg ? -1 : 1) * ((this.HandlePart(sp[0]) * 10000000000000000) +
(this.HandlePart(sp[1]) * 100000000) + this.HandlePart(sp[2])) +
dotPart;
}
else
{
if (yCount == 1)
{
pre = pre.Replace(“亿”, “,”);
string[] sp = pre.Split(‘,’);
result =(neg ? -1 : 1) * ((this.HandlePart(sp[0]) * 100000000) +
this.HandlePart(sp[1])) + dotPart;
}
else
{
if (yCount == 0)
{
result =(neg ? -1 : 1) * this.HandlePart(pre) + dotPart;
}
}
}
return result;

短信猫软件的完毕(C#)<八>7bitPDU的解码

}
/// <summary>
    /// 处理亿以下内容。
    /// </summary>
    /// <param name=”num”></param>
    /// <returns></returns>
private Decimal HandlePart(string num)
{
Decimal result = 0;
string temp = num;
temp = temp.Replace(“万”, “,”);
string[] part = temp.Split(‘,’);
for (int i = 0; i < part.Length; i++)
{
result +=Convert.ToDecimal(this.GetArabThousandPart(part[part.Length –
i – 1])) * Convert.ToDecimal((System.Math.Pow(10000,
Convert.ToDouble(i))));
}
return result;
}

}
/// <summary>
    /// 处理亿以下内容。
    /// </summary>
    /// <param name=”num”></param>
    /// <returns></returns>
private Decimal HandlePart(string num)
{
Decimal result = 0;
string temp = num;
temp = temp.Replace(“万”, “,”);
string[] part = temp.Split(‘,’);
for (int i = 0; i < part.Length; i++)
{
result +=Convert.ToDecimal(this.GetArabThousandPart(part[part.Length –
i – 1])) * Convert.ToDecimal((System.Math.Pow(10000,
Convert.ToDouble(i))));
}
return result;
}

短信猫软件的贯彻(C#)<九>7bitPDU的编码

/// <summary>
    /// 取得阿拉伯数字小数部分。
    /// </summary>
    /// <returns></returns>
private Decimal GetArabDotPart(string dotpart)
{
Decimal result = 0.00M;
string spe = “0.”;
for (int i = 0; i < dotpart.Length; i++)
{
spe +=this.switchNum(dotpart[i].ToString()).ToString();
}
result = Convert.ToDecimal(spe);
return result;
}

/// <summary>
    /// 取得阿拉伯数字小数部分。
    /// </summary>
    /// <returns></returns>
private Decimal GetArabDotPart(string dotpart)
{
Decimal result = 0.00M;
string spe = “0.”;
for (int i = 0; i < dotpart.Length; i++)
{
spe +=this.switchNum(dotpart[i].ToString()).ToString();
}
result = Convert.ToDecimal(spe);
return result;
}

短信猫软件的兑现(C#)<十>类库(API)完善

public int GetArabThousandPart(string number)
{

public int GetArabThousandPart(string number)
{

短信猫软件的贯彻(C#)<10一>软件达成(实现篇)

string ChnNumString = number;
if (ChnNumString == “零”)
{
return 0;
}
if (ChnNumString != string.Empty)
{
if (ChnNumString[0].ToString() == “十”)
{
ChnNumString = “一” + ChnNumString;
}
}

string ChnNumString = number;
if (ChnNumString == “零”)
{
return 0;
}
if (ChnNumString != string.Empty)
{
if (ChnNumString[0].ToString() == “十”)
{
ChnNumString = “一” + ChnNumString;
}
}

格外感激该我给大家无私的享用和平消除答,在运用的经过中,依旧察觉部分题材,短信发不出去,找来找去终于找到原因,我用的是卡塔尔多哈百亿科技(science and technology)的GSM短信猫,他有和好的壹些非凡规定,AT命令通用,但是求长度那1块类似差别,下边正是自家改正复原的短信猫操作类,增添了通话,获取具有音讯等艺术!

ChnNumString = ChnNumString.Replace(“零”, string.Empty);
//去除全体的零
int result = 0;
int index = ChnNumString.IndexOf(“千”);
if (index != -1)
{
result +=this.switchNum(ChnNumString.Substring(0, index)) * 1000;
ChnNumString =ChnNumString.Remove(0, index + 1);
}
index = ChnNumString.IndexOf(“百”);
if (index != -1)
{
result +=this.switchNum(ChnNumString.Substring(0 , index)) * 100;
ChnNumString = ChnNumString.Remove(0, index + 1);
}
index = ChnNumString.IndexOf(“十”);
if (index != -1)
{
result += this.switchNum(ChnNumString.Substring(0 , index)) * 10;
ChnNumString = ChnNumString.Remove(0, index + 1);
}
if (ChnNumString != string.Empty)
{
result += this.switchNum(ChnNumString);
}
return result;
}
/// <summary>
    /// 取得汉字对应的阿拉伯数字
    /// </summary>
    /// <param name=”n”></param>
    /// <returns></returns>
private int switchNum(string n)
{
switch (n)
{
case “零”:
{
return 0;
}
case “一”:
{
return 1;
}
case “二”:
{
return 2;
}
case “三”:
{
return 3;
}
case “四”:
{
return 4;
}
case “五”:
{
return 5;
}
case “六”:
{
return 6;
}
case “七”:
{
return 7;
}
case “八”:
{
return 8;
}
case “九”:
{
return 9;
}
default:
{
return -1;
}
}
}
}
}

ChnNumString = ChnNumString.Replace(“零”, string.Empty);
//去除全数的零
int result = 0;
int index = ChnNumString.IndexOf(“千”);
if (index != -1)
{
result +=this.switchNum(ChnNumString.Substring(0, index)) * 1000;
ChnNumString =ChnNumString.Remove(0, index + 1);
}
index = ChnNumString.IndexOf(“百”);
if (index != -1)
{
result +=this.switchNum(ChnNumString.Substring(0 , index)) * 100;
ChnNumString = ChnNumString.Remove(0, index + 1);
}
index = ChnNumString.IndexOf(“十”);
if (index != -1)
{
result += this.switchNum(ChnNumString.Substring(0 , index)) * 10;
ChnNumString = ChnNumString.Remove(0, index + 1);
}
if (ChnNumString != string.Empty)
{
result += this.switchNum(ChnNumString);
}
return result;
}
/// <summary>
    /// 取得汉字对应的阿拉伯数字
    /// </summary>
    /// <param name=”n”></param>
    /// <returns></returns>
private int switchNum(string n)
{
switch (n)
{
case “零”:
{
return 0;
}
case “一”:
{
return 1;
}
case “二”:
{
return 2;
}
case “三”:
{
return 3;
}
case “四”:
{
return 4;
}
case “五”:
{
return 5;
}
case “六”:
{
return 6;
}
case “七”:
{
return 7;
}
case “八”:
{
return 8;
}
case “九”:
{
return 9;
}
default:
{
return -1;
}
}
}
}
}

 

 

 

//类名:myGSMModem
//成效:短信猫类
//作者:刘典武革新(通用于费城百亿科技GSM)
//时间:2011-03-01
//表明:来源于乐乎(改正)

///页面调用的方式

///页面调用的方法

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO.Ports;

NumberConventer NC = new NumberConventer();//新建工作类对象
string TP = string.Empty;//TP转换结果
this.txtTotalPriceUP.Text =
NC.ArabToChn(Convert.ToDecimal(_TotalPrice), out
TP);//这一个办法再次回到结果,类型string,能处理的数字大小为一亿个亿

NumberConventer NC = new NumberConventer();//新建筑工程作类对象
string TP = string.Empty;//TP转换结果
this.txtTotalPriceUP.Text =
NC.ArabToChn(Convert.ToDecimal(_Total普赖斯), out
TP);//那么些主意重回结果,类型string,能处理的数字大小为1亿个亿

namespace myClassLibrary
{
    public enum MsgType { AUSC二, A7Bit };//枚举 短信类型 AUSC2A柒Bit:多个人编码 (汉语用AUSC2,英文都能够但七Bit能发送160字符,USC二仅70)
    public class myGSM
    {
        #region  构造函数
        /// <summary>
        /// 无参数构造函数 达成有关起初化工作
        /// </summary>
        public myGSM()
        {
            this.msgCenter = string.Empty;
            this.sp = new SerialPort();
            this.sp.ReadTimeout = -一;//读超时时间 发送短信时间的急需
            this.sp.LANDtsEnable = true;//必须为true
这样串口才能接受到数码
            this.sp.DataReceived += new
SerialDataReceivedEventHandler(this.sp_DataReceived);
        }

 

 

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name=”comPort”>串口号</param>
        /// <param name=”baudRate”>波特率</param>
        public myGSM(string comPort, int baudRate)
        {
            this.msgCenter = string.Empty;
            this.sp = new SerialPort();
            this.sp.PortName = comPort;
            this.sp.BaudRate = baudRate;
            this.sp.ReadTimeout = -壹;//读超时时间 发送短信时间的急需
            this.sp.MuranotsEnable = true;//必须为true
那样串口才能吸收接纳到多少
            this.sp.DataReceived += new
SerialDataReceivedEventHandler(this.sp_DataReceived);
        }
        #endregion

        #region  私有变量
        private SerialPort sp;
        private bool autoDelMsg = false;//是不是自动删除短新闻
        private string msgCenter = string.Empty;//服务宗旨号码
        private int newMsgIndex;//新音讯序号
        #endregion

        #region 属性
        /// <summary>
        /// 是还是不是自动删除短信(私下认可为false)
        /// </summary>
        public bool AutoDelMsg
        {
            get { return this.autoDelMsg; }
            set { this.autoDelMsg = value; }
        }

        /// <summary>
        /// 波特率 运维时只读 设备打开状态写入将吸引那么些
        /// </summary>
        public int BaudRate
        {
            get { return this.sp.BaudRate; }
            set { this.sp.BaudRate = value; }
        }

        /// <summary>
        /// 串口号 运营时只读 设备打开状态写入将吸引这个
        /// 提供对串口端口号的访问
        /// </summary>
        public string ComPort
        {
            get { return this.sp.PortName; }
            set { try { this.sp.PortName = value; } catch (Exception ex)
{ throw ex; } }
        }

        /// <summary>
        /// 设备是还是不是打开
        /// </summary>
        public bool IsOpen
        {
            get { return this.sp.IsOpen; }
        }

        #endregion

        #region  方法

        /// <summary>
        /// 设置服务大旨号码
        /// </summary>
        /// <param name=”msgCenterNo”></param>
        public void SetMsgCenterNo(string msgCenterNo)
        {
            this.msgCenter = msgCenterNo;
        }

        /// <summary>
        /// 获取机器码
        /// </summary>
        /// <returns></returns>
        public string GetMachineNo()
        {
            string temp = this.SendAT(“AT+CGMI”);
            if (temp.Substring(temp.Length – 4, 3).Trim() == “OK”)
            {
                temp = temp.Split(‘\r’)[2].Trim();
            }
            else
            {
                throw new Exception(“获取机器码退步”);
            }
            return temp;
        }

        /// <summary>
        /// 获取短信大旨号码
        /// </summary>
        /// <returns></returns>
        public string GetMsgCenterNo()
        {
            string temp = string.Empty;
            if (msgCenter != null && msgCenter.Length != 0)
            {
                return msgCenter;
            }
            else
            {
                temp = this.SendAT(“AT+CSCA?”);
                if (temp.Substring(temp.Length – 4, 3).Trim() == “OK”)
                {
                    return temp.Split(‘\”‘)[1].Trim();
                }
                else
                {
                    throw new Exception(“获取短信中央失利”);
                }
            }
        }

        /// <summary>
        /// 取得未读消息列表
        /// </summary>
        ///
<returns>未读消息列表(中央号码,手提式有线话机号码,发送时间,短信内容)</returns>
        public string[] GetUnReadMsg()
        {
            string[] result = new
string[255];//存款和储蓄25五条短信,1般手提式有线电话机方面存款和储蓄的短信少于那几个数
            string[] temp = null;
            string tt = string.Empty;

            tt = this.SendAT(“AT+CMGL=0”);//读取未读新闻
            if (tt.Substring(tt.Length – 4, 3).Trim() == “OK”)
            {
                temp = tt.Split(‘\r’);
            }
            PDUEncoding pe = new PDUEncoding();
            int i = 0;//计数
            foreach (string str in temp)
            {
                if (str != null && str.Length != 0 && str.Substring(0,
2).Trim() != “+C” && str.Substring(0, 2) != “OK” && str.Substring(0, 2)
!= “AT”)
                {
4858.com ,                    result[i] = pe.PDUDecoder(str);
                    i++;
                }
            }
            return result;
        }

        /// <summary>
        /// 取得已读新闻列表
        /// </summary>
        ///
<returns>已读音讯列表(中央号码,手提式无线电话机号码,发送时间,短信内容)</returns>
        public string[] GetIsReadMsg()
        {
            string[] result = new
string[255];//存款和储蓄255条短信,1般手提式有线电话机方面存款和储蓄的短信少于这一个数
            string[] temp = null;
            string tt = string.Empty;

            tt = this.SendAT(“AT+CMGL=1”);//读取已读消息
            if (tt.Substring(tt.Length – 4, 3).Trim() == “OK”)
            {
                temp = tt.Split(‘\r’);
            }
            PDUEncoding pe = new PDUEncoding();
            int i = 0;//计数
            foreach (string str in temp)
            {
                if (str != null && str.Length != 0 && str.Substring(0,
2).Trim() != “+C” && str.Substring(0, 2) != “OK” && str.Substring(0, 2)
!= “AT”)
                {
                    result[i] = pe.PDUDecoder(str);
                    i++;
                }
            }
            return result;
        }

        /// <summary>
        /// 取得待发新闻列表
        /// </summary>
        ///
<returns>待发新闻列表(中央号码,手机号码,发送时间,短信内容)</returns>
        public string[] GetUnSendMsg()
        {
            string[] result = new
string[255];//存款和储蓄255条短信,1般手提式有线电话机方面存款和储蓄的短信少于那么些数
            string[] temp = null;
            string tt = string.Empty;

            tt = this.SendAT(“AT+CMGL=二”);//读取待发音讯
            if (tt.Substring(tt.Length – 4, 3).Trim() == “OK”)
            {
                temp = tt.Split(‘\r’);
            }
            PDUEncoding pe = new PDUEncoding();
            int i = 0;//计数
            foreach (string str in temp)
            {
                if (str != null && str.Length != 0 && str.Substring(0,
2).Trim() != “+C” && str.Substring(0, 2) != “OK” && str.Substring(0, 2)
!= “AT”)
                {
                    result[i] = pe.PDUDecoder(str);
                    i++;
                }
            }
            return result;
        }

        /// <summary>
        /// 取得已发音讯列表
        /// </summary>
        ///
<returns>已发音信列表(中央号码,手提式有线电话机号码,发送时间,短信内容)</returns>
        public string[] GetIsSendMsg()
        {
            string[] result = new
string[255];//存款和储蓄25五条短信,1般手提式有线电话机方面存款和储蓄的短信少于这一个数
            string[] temp = null;
            string tt = string.Empty;

            tt = this.SendAT(“AT+CMGL=三”);//读取已发新闻
            if (tt.Substring(tt.Length – 4, 3).Trim() == “OK”)
            {
                temp = tt.Split(‘\r’);
            }
            PDUEncoding pe = new PDUEncoding();
            int i = 0;//计数
            foreach (string str in temp)
            {
                if (str != null && str.Length != 0 && str.Substring(0,
2).Trim() != “+C” && str.Substring(0, 2) != “OK” && str.Substring(0, 2)
!= “AT”)
                {
                    result[i] = pe.PDUDecoder(str);
                    i++;
                }
            }
            return result;
        }

        /// <summary>
        /// 获取具有新闻
        /// </summary>
        /// <returns></returns>
        public string[] GetAllMsg()
        {
            string[] result = new
string[255];//存款和储蓄25五条短信,一般手提式有线电话机方面存款和储蓄的短信少于那么些数
            string[] temp = null;
            string tt = string.Empty;

            tt = this.SendAT(“AT+C名爵L=四”);//读取未读音信
            if (tt.Substring(tt.Length – 4, 3).Trim() == “OK”)
            {
                temp = tt.Split(‘\r’);
            }
            PDUEncoding pe = new PDUEncoding();
            int i = 0;//计数
            foreach (string str in temp)
            {
                if (str != null && str.Length != 0 && str.Substring(0,
2).Trim() != “+C” && str.Substring(0, 2) != “OK” && str.Substring(0, 2)
!= “AT”)
                {
                    result[i] = pe.PDUDecoder(str);
                    i++;
                }
            }
            return result;
        }

        /// <summary>
        /// 读取设备新接受的短音讯
        /// </summary>
        /// <returns></returns>
        public string ReadNewMsg()
        {
            return ReadMsgByIndex(newMsgIndex);
        }

        /// <summary>
        /// 发送AT命令
        /// </summary>
        /// <param name=”ATCom”>AT命令</param>
        /// <returns></returns>
        public string SendAT(string ATCom)
        {
            string str = string.Empty;
            //忽略接收缓冲区内容,准备发送
            this.sp.DiscardInBuffer();
            //注销事件涉及,为发送做准备
            this.sp.DataReceived -= this.sp_DataReceived;
            try
            {
                this.sp.Write(ATCom + “\r”);
            }
            catch (Exception ex)
            {
                this.sp.DataReceived += this.sp_DataReceived;
                throw ex;
            }
            try
            {
                string temp = string.Empty;
                while ((temp.Trim() != “OK”) && (temp.Trim() !=
“ERROR”))
                {
                    temp = this.sp.ReadLine();
                    str += temp;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.sp.DataReceived += this.sp_DataReceived;
            }
            return str;
        }

        /// <summary>
        /// 发送短信       
        /// </summary>
        /// <param name=”phone”>手提式有线电电话机号码</param>
        /// <param name=”msg”>短信内容</param>
        public bool SendMsg(string phone, string msg)
        {
            string temp = “0011000D91” + this.reverserNumber(phone) +
“000801” + this.contentEncoding(msg) + Convert.ToChar(26).ToString();
            string len = this.getLenght(msg);//总结长度

            try
            {
                this.sp.DataReceived -= sp_DataReceived;
                this.sp.Write(“AT+CMGS=” + len + “\r”);
                this.sp.ReadTo(“>”);
                this.sp.DiscardInBuffer();
                //事件重新绑定 平常监视串口数据
                this.sp.DataReceived += sp_DataReceived;
                temp = this.SendAT(temp);
                if (temp.Substring(temp.Length – 4, 3).Trim() != “OK”)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch { return false; }
        }

        #region  自定义方法

        //获取短信内容的字节数
        private string getLenght(string txt)
        {
            int i = 0;
            string s = “”;
            i = txt.Length * 2;
            i += 15;
            s = i.ToString();
            return s;
        }
        //将手机号码转换为内部存款和储蓄器编码
        private string reverserNumber(string phone)
        {
            string str = “”;
            //检查手提式有线电话机号码是不是遵从专业格式写,倘若不是则补上
            if (phone.Substring(0, 2) != “86”)
            {
                phone = string.Format(“86{0}”, phone);
            }
            char[] c = this.getChar(phone);
            for (int i = 0; i <= c.Length – 2; i += 2)
            {
                str += c[i + 1].ToString() + c[i].ToString();
            }
            return str;
        }
        //汉字解码为16进制
        private string contentEncoding(string content)
        {
            Encoding encodingUTF =
System.Text.Encoding.BigEndianUnicode;
            string s = “”;
            byte[] encodeByte = encodingUTF.GetBytes(content);
            for (int i = 0; i <= encodeByte.Length – 1; i++)
            {
                s += BitConverter.ToString(encodeByte, i, 1);
            }
            s = string.Format(“{0:X2}{1}”, s.Length / 2, s);
            return s;
        }

        private char[] getChar(string phone)
        {
            if (phone.Length % 2 == 0)
            {
                return Convert.ToString(phone).ToCharArray();
            }
            else
            {
                return Convert.ToString(phone + “F”).ToCharArray();
            }
        }
        #endregion

        /// <summary>
        /// 发送短信 (重载)
        /// </summary>
        /// <param name=”phone”>手提式有线电电话机号码</param>
        /// <param name=”msg”>短信内容</param>
        /// <param name=”msgType”>短信项目</param>
        public void SendMsg(string phone, string msg, MsgType msgType)
        {
            if (msgType == MsgType.AUSC2)
            {
                SendMsg(phone, msg);
            }
            else
            {

                PDUEncoding pe = new PDUEncoding();
                pe.ServiceCenterAddress = msgCenter;                   
//短信核心号码 服务为主地址

                string temp = pe.PDU7BitEncoder(phone, msg);

                int len = (temp.Length –
Convert.ToInt32(temp.Substring(0, 2), 16) * 2 – 二) / 二;  //总括长度
                try
                {
                    temp = SendAT(“AT+CMGS=” + len.ToString() + “\r” +
temp + (char)(26));  //26 Ctrl+Z ascii码
                }
                catch (Exception)
                {
                    throw new Exception(“短信发送退步”);
                }

                if (temp.Substring(temp.Length – 4, 3).Trim() == “OK”)
                {
                    return;
                }

                throw new Exception(“短信发送失利”);
            }
        }

        /// <summary>
        /// 关闭设备
        /// </summary>
        public void CloseComm()
        {
            try
            {
                this.sp.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 打开设备
        /// </summary>
        public void OpenComm()
        {
            try
            {
                this.sp.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (this.sp.IsOpen)
            {
                //this.sp.DataReceived -= this.sp_DataReceived;
                //this.sp.Write(“AT\r”);
                //Thread.Sleep(100);
                //string s = this.sp.ReadExisting().Trim();
                //s = s.Substring(s.Length – 2, 2);
                //if (s != “OK”)
                //{
                //    throw new Exception(“硬件连接错误”);
                //}

                try
                {
                    this.SendAT(“AT+CMGF=0”);//选用短音讯格式私下认可为PDU
                    Thread.Sleep(100);
                   
this.SendAT(“AT+CNMI=二,壹”);//选拔当有新短音信来时提示格局
                    Thread.Sleep(100);
                }
                catch { }
            }
        }

        /// <summary>
        /// 拨打电话
        /// </summary>
        /// <param name=”telNum”>电话号码</param>
        public void Call(string telNum)
        {
            try
            {
                this.SendAT(“ATD” + telNum + “;”);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 按序号读取短信
        /// </summary>
        /// <param name=”index”>序号</param>
        /// <param name=”msgCenter”>短信中央</param>
        /// <param name=”phone”>发送方手机号码</param>
        /// <param name=”msg”>短信内容</param>
        /// <param name=”time”>时间字符串</param>
        public void ReadMsgByIndex(int index, out string msgCenter, out
string phone, out string msg, out string time)
        {
            string temp = string.Empty;
            PDUEncoding pe = new PDUEncoding();
            try
            {
                temp = SendAT(“AT+CMGR=” + index.ToString() + “\r”);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (temp.Trim() == “ERROR”)
            {
                throw new Exception(“没有此短信”);
            }
            temp = temp.Split((char)(13))[2];      
//取出PDU串(char)(13)为0x0a即\r 按\r分为多个字符串 第一个是PDU串

            pe.PDUDecoder(temp, out msgCenter, out phone, out msg, out
time);
        }

        /// <summary>
        /// 按序号读取短信
        /// </summary>
        /// <param name=”index”>序号</param>
        /// <returns>信息字符串
(大旨号码,手提式有线电电话机号码,发送时间,短信内容)</returns>
        public string ReadMsgByIndex(int index)
        {
            string temp = string.Empty;
            string msgCenter, phone, msg, time;
            PDUEncoding pe = new PDUEncoding();
            try
            {
                temp = SendAT(“AT+CMGR=” + index.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (temp.Trim() == “ERROR”)
            {
                throw new Exception(“未有此短信”);
            }

            temp = temp.Split((char)(13))[3];      
//取出PDU串(char)(13)为0x0a即\r 按\r分为八个字符串 第二个是PDU串

            pe.PDUDecoder(temp, out msgCenter, out phone, out msg, out
time);

            if (AutoDelMsg)//若是阅读完短信自动删除设置为真
            {
                try
                {
                    DelMsgByIndex(index);
                }
                catch { }
            }
            return msgCenter + “,” + phone + “,” + time + “,” + msg;
        }

        /// <summary>
        /// 删除对应序号短信
        /// </summary>
        /// <param name=”index”>短信序号</param>
        /// <returns></returns>
        public bool DelMsgByIndex(int index)
        {
            if (SendAT(“AT+CMGD=” + index.ToString()).Trim() == “OK”)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        /// <summary>
        /// 创立事件接受音讯的嘱托
        /// </summary>
        /// <param name=”sender”></param>
        /// <param name=”e”></param>
        public delegate void OnRecievedHandler(object sender, EventArgs
e);

        /// <summary>
        /// 收到短音讯事件 OnRecieved
        /// 收到短信将抓住此事件
        /// </summary>
        public event OnRecievedHandler GetNewMsg;

        private void sp_DataReceived(object sender,
SerialDataReceivedEventArgs e)
        {
            string str = this.sp.ReadLine();
            if ((str.Length > 8) && (str.Substring(0, 6) ==
“+CMTI:”))
            {
                this.newMsgIndex =
Convert.ToInt32(str.Split(‘,’)[1]);
                this.GetNewMsg(this, e);
            }
        }
    }

    class PDUEncoding
    {
        private string serviceCenterAddress = “00”;
        /// <summary>
        /// 音信服务核心(壹-十三个8人组)
        /// </summary>
        public string ServiceCenterAddress
        {
            get
            {
                int len = 2 *
Convert.ToInt32(serviceCenterAddress.Substring(0, 2));
                string result = serviceCenterAddress.Substring(4, len –
2);

                result = ParityChange(result);
                result = result.TrimEnd(‘F’, ‘f’);
                return result;
            }
            set                 //
            {
                if (value == null || value.Length == 0)     
//号码为空
                {
                    serviceCenterAddress = “00”;
                }
                else
                {
                    if (value[0] == ‘+’)
                    {
                        value = value.TrimStart(‘+’);
                    }
                    if (value.Substring(0, 2) != “86”)
                    {
                        value = “86” + value;
                    }
                    value = “91” + ParityChange(value);
                    serviceCenterAddress = (value.Length /
2).ToString(“X2”) + value;
                }

            }
        }

        private string protocolDataUnitType = “11”;
        /// <summary>
        /// 协议数据单元类型(三个6人组)
        /// </summary>
        public string ProtocolDataUnitType
        {
            set
            {

            }
            get
            {
                return “11”;
            }
        }

        private string messageReference = “00”;
        /// <summary>
        /// 全体成功的短信发送参考数目(0..25伍)
        /// (1个8位组)
        /// </summary>
        public string MessageReference
        {
            get
            {
                return “00”;
            }
        }

        private string originatorAddress = “00”;
        /// <summary>
        /// 发送方地址(手提式有线话机号码)(二-十二个六个人组)
        /// </summary>
        public string OriginatorAddress
        {
            get
            {
                int len = Convert.ToInt3二(originatorAddress.Substring(0,
贰), 1陆);    //十陆进制字符串转为整形数据
                string result = string.Empty;
                if (len % ② == 一)       //号码长度是奇数,长度加一编码时加了F
                {
                    len++;
                }
                result = originatorAddress.Substring(4, len);
                result = ParityChange(result).TrimEnd(‘F’, ‘f’);   
//奇偶交流,并去掉结尾F

                return result;
            }
        }

        private string destinationAddress = “00”;
        /// <summary>
        /// 接收方地址(手提式有线话机号码)(二-十三个五人组)
        /// </summary>
        public string DestinationAddress
        {
            set
            {
                if (value == null || value.Length == 0)     
//号码为空
                {
                    destinationAddress = “00”;
                }
                else
                {
                    if (value[0] == ‘+’)
                    {
                        value = value.TrimStart(‘+’);
                    }
                    if (value.Substring(0, 2) == “86”)
                    {
                        value = value.TrimStart(‘8’, ‘6’);
                    }
                    int len = value.Length;
                    value = ParityChange(value);

                    destinationAddress = len.ToString(“X2”) + “A1” +
value;
                }
            }
        }

        private string protocolIdentifer = “00”;
        /// <summary>
        /// 参数字突显示音讯主旨以何种措施处理音讯内容
        /// (比如FAX,Voice)(1个8位组)
        /// </summary>
        public string ProtocolIdentifer
        {
            get
            {
                return protocolIdentifer;
            }
            set
            {

            }
        }

        private string dataCodingScheme = “0捌”;    
//权且仅补助国内USC二编码
        /// <summary>
        /// 参数字展现示用户数据编码方案(一个六人组)
        /// </summary>
        public string DataCodingScheme
        {
            get
            {
                return dataCodingScheme;
            }
        }

        private string serviceCenterTimeStamp = “”;
        /// <summary>
        /// 新闻中央吸收接纳音讯时的光阴戳(八个5人组)
        /// </summary>
        public string ServiceCenterTimeStamp
        {
            get
            {
                string result = ParityChange(serviceCenterTimeStamp);
                result = “20” + result.Substring(0, 1二);           
//年加初叶的“20”

                return result;
            }
        }

        private string validityPeriod = “C四”;       //近年来固定有效期
        /// <summary>
        /// 短新闻有效期(0,一,8个5位组)
        /// </summary>
        public string ValidityPeriod
        {
            get
            {
                return “C4”;
            }
        }

        private string userDataLenghth = “”;
        /// <summary>
        /// 用户数量长度(三个七人组)
        /// </summary>
        public string UserDataLenghth
        {
            get
            {
                return (userData.Length / 2).ToString(“X2”);
            }
        }

        private string userData = “”;
        /// <summary>
        /// 用户数据(0-1三十八个5个人组)
        /// </summary>
        public string UserData
        {
            get
            {
                int len = Convert.ToInt32(userDataLenghth, 16) * 2;
                string result = string.Empty;

                if (dataCodingScheme == “08” || dataCodingScheme ==
“18”)             //USC2编码
                {
                    //多少个壹组,每组译为一个USC二字符
                    for (int i = 0; i < len; i += 4)
                    {
                        string temp = userData.Substring(i, 4);

                        int byte1 = Convert.ToInt16(temp, 16);

                        result += ((char)byte1).ToString();
                    }
                }
                else
                {
                    result = PDU7bitDecoder(userData);
                }

                return result;
            }
            set
            {
                userData = string.Empty;
                Encoding encodingUTF = Encoding.BigEndianUnicode;

                byte[] Bytes = encodingUTF.GetBytes(value);

                for (int i = 0; i < Bytes.Length; i++)
                {
                    userData += BitConverter.ToString(Bytes, i, 1);
                }
                userDataLenghth = (userData.Length /
2).ToString(“X2”);
            }
        }

        /// <summary>
        /// 奇偶交流 (+F)
        /// </summary>
        /// <param name=”str”>要被转换的字符串</param>
        /// <returns>转换后的结果字符串</returns>
        private string ParityChange(string str)
        {
            string result = string.Empty;

            if (str.Length % 2 != 0)         //奇字符串 补F
            {
                str += “F”;
            }
            for (int i = 0; i < str.Length; i += 2)
            {
                result += str[i + 1];
                result += str[i];
            }

            return result;
        }

        /// <summary>
        /// PDU编码器,完成PDU编码(USC2编码,最多70个字)
        /// </summary>
        /// <param name=”phone”>目标手提式有线电电话机号码</param>
        /// <param name=”Text”>短信内容</param>
        /// <returns>编码后的PDU字符串</returns>
        public string PDUEncoder(string phone, string Text)
        {
            if (Text.Length > 70)
            {
                throw (new Exception(“短信字数当先70”));
            }
            DestinationAddress = phone;
            UserData = Text;

            return serviceCenterAddress + protocolDataUnitType
                + messageReference + destinationAddress +
protocolIdentifer
                + dataCodingScheme + validityPeriod + userDataLenghth +
userData;
        }

        /// <summary>
        /// 7bit 编码
        /// </summary>
        /// <param name=”phone”>手提式有线电电话机号码</param>
        /// <param name=”Text”>短信内容</param>
        /// <returns>编码后的字符串</returns>
        public string PDU7BitEncoder(string phone, string Text)
        {
            if (Text.Length > 160)
            {
                throw new Exception(“短信字数大于160”);
            }
            dataCodingScheme = “00”;
            DestinationAddress = phone;
            UserData = Text;

            return serviceCenterAddress + protocolDataUnitType
                + messageReference + destinationAddress +
protocolIdentifer
                + dataCodingScheme + validityPeriod + userDataLenghth +
userData;
        }

        /// <summary>
        /// 重载 解码,重临音讯字符串
        /// </summary>
        /// <param name=”strPDU”>短信PDU字符串</param>
        ///
<returns>音讯字符串(中央号码,手提式有线话机号码,发送时间,短信内容)</returns>
        public string PDUDecoder(string strPDU)
        {
            int length = (Convert.ToInt32(strPDU.Substring(0, 2), 0x10)
* 2) + 2;
            this.serviceCenterAddress = strPDU.Substring(0, length);
            int num2 = Convert.ToInt32(strPDU.Substring(length + 2, 2),
0x10);
            if ((num2 % 2) == 1)
            {
                num2++;
            }
            num2 += 4;
            this.originatorAddress = strPDU.Substring(length + 2,
num2);
            this.dataCodingScheme = strPDU.Substring((length + num2) +
4, 2);
            this.serviceCenterTimeStamp = strPDU.Substring((length +
num2) + 6, 14);
            this.userDataLenghth = strPDU.Substring((length + num2) +
20, 2);
            Convert.ToInt32(this.userDataLenghth, 0x10);
            this.userData = strPDU.Substring((length + num2) + 0x16);
            return (this.ServiceCenterAddress + “,” +
this.OriginatorAddress + “,” + this.ServiceCenterTimeStamp + “,” +
this.UserData);
        }

        /// <summary>
        /// 达成手提式有线电话机或短信猫收到PDU格式短信的解码 暂且仅扶助粤语编码
        /// 未用DCS部分
        /// </summary>
        /// <param name=”strPDU”>短信PDU字符串</param>
        /// <param name=”msgCenter”>短消息服务大旨输出</param>
        /// <param name=”phone”>发送方手提式有线电电话机号码
输出</param>
        /// <param name=”msg”>短信内容 输出</param>
        /// <param name=”time”>时间字符串 输出</param>
        public void PDUDecoder(string strPDU, out string msgCenter, out
string phone, out string msg, out string time)
        {
            int lenSCA = Convert.ToInt32(strPDU.Substring(0, 2), 16) *
二 + 二;       //短音信宗旨占长度
            serviceCenterAddress = strPDU.Substring(0, lenSCA);

            int lenOA = Convert.ToInt32(strPDU.Substring(lenSCA + 2, 二),
1陆);           //OA占用长度
            if (lenOA % 二 ==
一)                                                     //奇数则加1 F位
            {
                lenOA++;
            }
            lenOA += 四;                 //加号码编码的头顶长度
            originatorAddress = strPDU.Substring(lenSCA + 2, lenOA);

            dataCodingScheme = strPDU.Substring(lenSCA + lenOA + 四,
2);             //DCS赋值,区分解码柒bit

            serviceCenterTimeStamp = strPDU.Substring(lenSCA + lenOA +
6, 14);

            userDataLenghth = strPDU.Substring(lenSCA + lenOA + 20,
2);
            int lenUD = Convert.ToInt32(userDataLenghth, 16) * 2;
            userData = strPDU.Substring(lenSCA + lenOA + 22);

            msgCenter = ServiceCenterAddress;
            phone = OriginatorAddress;
            msg = UserData;
            time = ServiceCenterTimeStamp;
        }

        /// <summary>
        /// PDU7bit的解码,供UserData的get访问器调用
        /// </summary>
        /// <param name=”len”>用户数据长度</param>
        /// <param
name=”userData”>数据部分PDU字符串</param>
        /// <returns></returns>
        private string PDU7bitDecoder(string userData)
        {
            string result = string.Empty;
            byte[] b = new byte[100];
            string temp = string.Empty;

            for (int i = 0; i < userData.Length; i += 2)
            {
                b[i / 2] =
(byte)Convert.ToByte((userData[i].ToString() + userData[i +
1].ToString()), 16);
            }

            int j = 0;            //while计数
            int tmp = 一;            //temp中二进制字符字符个数
            while (j < userData.Length / 2 – 1)
            {
                string s = string.Empty;

                s = Convert.ToString(b[j], 2);

                while (s.Length < 八)            //s补满5个人byte转化来的 有的不足6位,直接解码将招致错误
                {
                    s = “0” + s;
                }

                result += (char)Convert.ToInt3贰(s.Substring(tmp) + temp,
二);        //参与二个字符 结果集 temp 上一个人组剩余

                temp = s.Substring(0, tmp);            
//前1个人组多的局地

                if (tmp > 6)                           
//多余的部分满四人,到场八个字符
                {
                    result += (char)Convert.ToInt32(temp, 2);
                    temp = string.Empty;
                    tmp = 0;
                }

                tmp++;
                j++;

                if (j == userData.Length / 2 – 1)          
//最终二个字符
                {
                    result +=
(char)Convert.ToInt32(Convert.ToString(b[j], 2) + temp, 2);
                }
            }
            return result;
        }
    }
}

 

 

 

 

 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有