中国移动短信网关CMPP3.0 C#源代码:事件定义及相关工具函数

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Collections;
using System.Diagnostics;
namespace Tiray.SMS
{
 //事件参数定义
 public class SMSEventArgs:EventArgs
 {
  SMS_STATE m_State;
  Object m_Data;
  DateTime m_dtTime;

  public SMS_STATE State
  {
   get{return m_State;}
   set{m_State=value;}
  }

  public object Data
  {
   get{return m_Data;}
   set{m_Data=value;}
  }
  public DateTime Time
  {
   get{return m_dtTime;}
   set{m_dtTime=value;}
  }

 }
 //事件处理函数
 public delegate void SMSEventHandler(object sender,SMSEventArgs e);
 
 //异步事件回调函数
 public delegate void SMSAsyncEvent(SMSEventArgs e);

 public class Utility
 {
  public static String Decode(Byte[] buf,int StartIndex,int Length,CODING Coding)
  {
   String str=String.Empty;
   if(Coding==CODING.ASCII)
    str=System.Text.Encoding.ASCII.GetString(buf,StartIndex,Length);
   
   else if(Coding==CODING.UCS2)
    str=System.Text.Encoding.BigEndianUnicode.GetString(buf,0,Length);
   else if(Coding==CODING.GBK)
    str=System.Text.UnicodeEncoding.GetEncoding("gb2312").GetString(buf,StartIndex,Length);
   return str;
  }
  public static Byte[] Encode(String str,CODING coding)
  {
   Byte[] buf=null;
   if(str==null)
    return buf;
   if(coding==CODING.ASCII)
    buf=System.Text.Encoding.ASCII.GetBytes(str);
   else if(coding==CODING.UCS2)
    buf=System.Text.Encoding.BigEndianUnicode.GetBytes(str);
   else if(coding==CODING.GBK)
    buf=System.Text.UnicodeEncoding.GetEncoding("gb2312").GetBytes(str);

   return buf;

  }
  public static UInt32 CountLength(String str,CODING coding)
  {
   Byte [] buf=Encode(str,coding);
   if(buf!=null)
    return (UInt32)buf.Length;
   else
    return 0;
  }

  public static Byte[] IntToNetBytes(object obj)
  {
   Byte[] bytes=null;
   if(obj.GetType()==System.Type.GetType("System.UInt32"))
   {
    UInt32 val=(UInt32) obj;
    bytes=BitConverter.GetBytes(val);
   }
   if(obj.GetType()==System.Type.GetType("System.UInt64"))
   {
    UInt64 val=(UInt64) obj;
    bytes=BitConverter.GetBytes(val);
   }

   if(bytes!=null)
    System.Array.Reverse(bytes);

   return bytes;

  }
  public static object NetBytesToInt(Byte[] bytes,int index,int length)
  {
   Array.Reverse(bytes,index,length);
   if(length==4)
    return BitConverter.ToUInt32(bytes,index);
   else if(length==8)
    return BitConverter.ToUInt64(bytes,index);
   else
    return 0;
  }
 }

}

【待续】

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值