短信专用编码格式转换(如cmpp,sgip)

public enum CODING
{
ASCII=0,BINARY=4,UCS2=8,GBK=15
}
public static String Decode(Byte[] buf,int StartIndex,int Length,CODING Coding)
{
String str=String.Empty;
if(Coding==CODING.ASCII)
{
System.Text.ASCIIEncoding AsciiEncoder=new System.Text.ASCIIEncoding();
str=new String(AsciiEncoder.GetChars(buf,StartIndex,Length));
str=str.Split('/0')[0];

}
else if(Coding==CODING.UCS2)
{
Byte[] temp=new Byte[Length];
for(int i=0; i<Length;)
{
// 高低位字节对调
temp[i] =buf[StartIndex+i+1];
temp[i+1]= buf[StartIndex+i];
i=i+2;
}
System.Text.UnicodeEncoding UnicodeEncoder=new System.Text.UnicodeEncoding();
str=new String(UnicodeEncoder.GetChars(temp,0,Length));
str=str.Split('/0')[0];
}
else if(Coding==CODING.GBK)
{
System.Text.UnicodeEncoding Encoder=new System.Text.UnicodeEncoding();
System.Text.Encoding en=System.Text.UnicodeEncoding.GetEncoding("gb2312");
str=new String(en.GetChars(buf,StartIndex,Length));
str=str.Split('/0')[0];

}
return str;
}
public static Byte[] Encode(String str,CODING coding)
{
Byte[] buf=null;
if(coding==CODING.ASCII)
{
System.Text.ASCIIEncoding AsciiEncoder=new System.Text.ASCIIEncoding();
buf=AsciiEncoder.GetBytes(str);
}
else if(coding==CODING.UCS2)
{
System.Text.UnicodeEncoding UnicodeEncoder=new System.Text.UnicodeEncoding();
buf=UnicodeEncoder.GetBytes(str);
Byte temp=0;
for(int i=0; i<buf.Length;)
{
// 高低位字节对调
temp=buf[i] ;
buf[i]= buf[i+1];
buf[i+1]=temp;
i=i+2;
}
}
else if(coding==CODING.GBK)
{
System.Text.Encoding en=System.Text.UnicodeEncoding.GetEncoding("gb2312");
buf=en.GetBytes(str);
}

return buf;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值