public static string GetStringFromBytes(byte[] data, DataCoding dataCoding) { if (data == null) { throw new ArgumentNullException("data"); } string result = null; switch (dataCoding) { case DataCoding.ASCII: result = System.Text.Encoding.ASCII.GetString(data); break; case DataCoding.Latin1: result = Latin1Encoding.GetString(data); break; case DataCoding.SMSCDefault: result = SMSCDefaultEncoding.GetString(data); break; case DataCoding.UCS2: result = UCS2Encoding.GetString(data); break; default: throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding"); } //Since a CString may contain a null terminating charactor //Replace all occurences of null charactors return(result.Replace("\u0000", "")); }
public static byte[] GetBytesFromString(string cStr, DataCoding dataCoding) { if (cStr == null) { throw new ArgumentNullException("cStr"); } if (cStr.Length == 0) { return(new byte[] { 0x00 }); } byte[] bytes = null; switch (dataCoding) { case DataCoding.ASCII: bytes = System.Text.Encoding.ASCII.GetBytes(cStr); break; case DataCoding.Latin1: bytes = Latin1Encoding.GetBytes(cStr); break; case DataCoding.UCS2: bytes = UCS2Encoding.GetBytes(cStr); break; case DataCoding.SMSCDefault: bytes = SMSCDefaultEncoding.GetBytes(cStr); break; default: throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding"); } return(bytes); }
private static byte[] EncodeString(DataCoding dataCoding, string str) { byte[] bytes; switch (dataCoding) { case DataCoding.Ascii: bytes = Encoding.ASCII.GetBytes(str); break; case DataCoding.Latin1: bytes = Latin1Encoding.GetBytes(str); break; case DataCoding.Ucs2: bytes = Encoding.BigEndianUnicode.GetBytes(str); break; case DataCoding.SmscDefault: bytes = SmscDefaultEncoding.GetBytes(str); break; default: throw new SmppException(SmppErrorCode.EsmeRunknownerr, "Unsupported encoding"); } return(bytes); }
private static string DecodeString(byte[] data, DataCoding dataCoding) { string result; switch (dataCoding) { case DataCoding.Ascii: result = Encoding.ASCII.GetString(data); break; case DataCoding.Latin1: result = Latin1Encoding.GetString(data); break; case DataCoding.Ucs2: result = Encoding.BigEndianUnicode.GetString(data); break; case DataCoding.SmscDefault: result = SmscDefaultEncoding.GetString(data); break; default: throw new SmppException(SmppErrorCode.EsmeRunknownerr, "Unsupported encoding"); } return(result); }
public static string GetCStringFromBytes(byte[] data, DataCoding dataCoding) { if (data == null) { throw new ArgumentNullException("data"); } if (data.Length < 1) { throw new ArgumentException("Array cannot be empty", "data"); } if (data[data.Length - 1] != 0x00) { throw new ArgumentException("CString must be terminated with a null charactor", "data"); } if (data.Length == 1) { return(""); } //The string is empty if it contains a single null charactor string result = null; switch (dataCoding) { case DataCoding.ASCII: result = System.Text.Encoding.ASCII.GetString(data); break; case DataCoding.Latin1: result = Latin1Encoding.GetString(data); break; case DataCoding.SMSCDefault: result = SMSCDefaultEncoding.GetString(data); break; case DataCoding.UCS2: result = UCS2Encoding.GetString(data); break; default: throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding"); } return(result.Replace("\x00", ""));//Replace the terminating null charactor }
public static byte[] GetBytesFromCString(string cStr, DataCoding dataCoding) { if (cStr == null) { throw new ArgumentNullException("cStr"); } if (cStr.Length == 0) { return(new byte[] { 0x00 }); } byte[] bytes = null; switch (dataCoding) { case DataCoding.ASCII: bytes = System.Text.Encoding.ASCII.GetBytes(cStr); break; case DataCoding.Latin1: bytes = Latin1Encoding.GetBytes(cStr); break; case DataCoding.UCS2: bytes = UCS2Encoding.GetBytes(cStr); break; case DataCoding.SMSCDefault: bytes = SMSCDefaultEncoding.GetBytes(cStr); break; default: throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding"); } ByteBuffer buffer = new ByteBuffer(bytes, bytes.Length + 1); buffer.Append(new byte[] { 0x00 }); //Append a null charactor a the end return(buffer.ToBytes()); }