Using system; using system.text;
Namespace cn.smslib {/// /// by popcorn 2004.5. /// cnpopcorn@hotmail.com /// summary> public class cntext {public cntext () {} /// /// encoding format /// summary> public enum gsmcode {bit7 = 0 , Bit8 = 1, UCS2 = 2}
/// /// Decoding the entire short message /// summary> /// Information to decode param> /// Decoded phone number param> /// Decoded SMS content param> /// SMS timestamp param> / // Used encoding param> /// successfully returns true returns> static public bool decodingmsg (String S, Ref string phone, ref string text, ref datetime sendtime , Ref strcode code, ref string sca) {Try {// Short message center INLEngth = int.Parse (s.Substring (0, 2), system.globalization.numberStyles.allowHexSpecifier; if (iLength> 0) {IF (S.SUBSTRING (2, 2) == "91") {SCA = " "; ilength--;} for (int i = 0; i
s = s.Remove (0, Ilength * 2 6);
// Transmitter number ilength = int.parse (s.Substring (0, 2), system.globalization.numberStyles.allowHexSpecifier; if (s.substring (2, 2) == "91") {phone = " "} If (ilength% 2 == 1) iLEngth ; for (int i = 0; i
S = S.Remove (0, 2); // Timestamp Sendtime = New DateTime (INT.PARSE ("20" S.SUBSTRING (1, 1) S.SUBSTRING (0, 1)), int.Parse (S.Substring (3, 1) S.Substring (2, 1)), int.parse (s.Substring (5, 1) s.Substring (4, 1)), int.parse (S.Substring (7, 1) S.SUBSTRING (6, 1)), int.Pars (s.Substring (9, 1) s.Substring (8, 1)), int.Pars (11, 1 ) s.Substring (10, 1)))); s = s.Remove (0,16);
// Received information if (code == gsmcode.bit7) {text = decodingbit7 (s);} else if (code == gsmcode.ucs2) {text = decodingucs2 (s);} else {text = decodingbit8 (s } Return True;} catCh {returnaf false;}}
/// /// The short message center is encoded /// summary> /// Number to encode param> /// encoded Number returns> static public string encodingsca (string s) {stringbuilder sb = new stringbuilder (); if (s.Length == 0) {sb.append ("00"); return sb.tostring ();} if (S.StartSwith) {sb.append ("91"); // with international format numbers (add ' ') s = s.Remove (0, 1);} else {SB. Append ("c8");} if (s.Length% 2 == 1) s = "f"; for (int i = 0; i /// Code /// summary> /// Code param> /// encoding The post-phone number returns> static public string encodingNumber (string mobileno) {stringbuilder sb = new stringbuilder (); if (Mobileeno.startSwith (" "))) {sb.append ("91"); MobileNo = Mobileno. REMOVE (0, 1);} else {sb.append ("c8"); IF (MobileNo.Length% 2 == 1) Mobileno = "f"; for (int i = 0; i
/// /// Using 7-Bit /// summary> /// English string param> //// information to encode Length and encoded string return s (string s) {int it = 0; string sreturn = ""; stringbuilder sb = new stringbuilder (); for (int i = 0; i > iChar; // Modify the pointer and count value PDST of the target string; sb.append (Sreturn.Substring (2, 2 ));}} Sb.append (sreturn.substring (0, 2)); return (sb.Length / 2) .tostring ("x2") sb.toString ();} /// // / Decoding /// summary> /// Decoded string param> /// Decoded English string returns > Static public string decodingbit7 (String S) {INT IBYTE = 0; INT ILEFT = 0; // Divide the source data into a set, unzipped into 8 bytes // looped the process, until the source data is processed // If grouping Less than 7 bytes, you can also correctly handle system.text.text.StringBuilder (); for (int i = 0; i
Saved as residual data (7-ibyte); // Modify the byte count value ibyte ; // to a set of last byte IF (ibyte == 7) {// extra target decoding Byte sb.append (ileft.tostring ("x2"))); // Group byte sequence and residual data initialize ibyte = 0; ►} string sreturn = sb.tostring (); byte [] buf = New byte [Srete [Srete [Srete]; for (int i = 0; i /// ///////////// summary> //// Summary> //// Param name = "s"> String param> /// information length and encoded string returns> static public string encodingbit8 (string s) {StringBuilder SB = New StringBuilder () Byte [] buf = encoding.ascii.getbytes (s); sb. fant (buf.Length.tostring ("x2")); for (int i = 0; i /// Using 8-Bit to decode /// summary> //// Strings to decode param> /// Decoded string returns> static public string decodingbit8 (string s) {byte [] buf = new byte [S.Length / 2]; stringbuildth / 2]; stringbuilder sb = new stringbuilder (); for (int i = 0; i < S.Length; i = 2) {BUF [I / 2] = Byte.Parse (S.Substring (i, 2), system.globalization.numberStyles.AllowHexSpecifier;} Return Encoding.ASCII.GetString (buf);}
/// /// Chinese short information UCS2 encoding /// summary> /// Chinese string param> /// information length to encode And encoded strings returns> static public string encodingucs2 (string s) {stringbuildier sb = new stringbuilder (); Byte [] buf = encoding.uitode.getbytes (s); sb.Append (buf.Length.tostring ("X2")); for (int i = 0; i /// Chinese short message UCS2 decoding /// summary> /// Information to decode param> /// Decoded Chinese string returns> static public string decodingucs2 (string s) {byte [] buf = new byte [S.LENGTH]; INT i = 0; i
}