/// <summary> /// Modifies the message text so that it is safe to be sent via GSM 7-Bit default encoding. /// </summary> /// <param name="data">The message text.</param> /// <param name="charsCorrected">Will be set to true if the message length was corrected.</param> /// <param name="lengthCorrected">Will be set to true if one or more characters were replaced.</param> /// <returns>The converted message text.</returns> /// <remarks>Replaces invalid characters in the text and truncates it to the maximum allowed length.</remarks> public static string GetSafeText(string data, out bool lengthCorrected, out bool charsCorrected) { string str; string str1; bool flag = false; lengthCorrected = false; charsCorrected = false; if (data.Length <= 160) { str = data; } else { str = data.Substring(0, 160); lengthCorrected = true; } do { str1 = TextDataConverter.StringTo7Bit(str, false, out flag); if (flag) { charsCorrected = true; } if (str1.Length <= 160) { continue; } str = data.Substring(0, str.Length - 1); lengthCorrected = true; }while (str1.Length > 160); string str2 = TextDataConverter.SevenBitToString(str1); return(str2); }
/// <summary> /// Encodes the specified text as 7-Bit user data in this instance. /// </summary> /// <param name="text">The text to encode.</param> /// <remarks>The text is converted to the GSM 7-Bit default alphabet first, then it is packed into octets. /// The final result is saved in the properties <see cref="P:GsmComm.PduConverter.SmsPdu.UserData" /> and <see cref="P:GsmComm.PduConverter.SmsPdu.UserDataLength" />. /// </remarks> /// <exception cref="T:System.ArgumentException">Text is too long.</exception> protected void Encode7BitText(string text) { string str = TextDataConverter.StringTo7Bit(text); int length = str.Length; if (length <= 160) { this.SetUserData(TextDataConverter.SeptetsToOctetsInt(str), (byte)length); return; } else { string[] strArrays = new string[5]; strArrays[0] = "Text is too long. A maximum of "; int num = 160; strArrays[1] = num.ToString(); strArrays[2] = " resulting septets is allowed. The current input results in "; strArrays[3] = length.ToString(); strArrays[4] = " septets."; throw new ArgumentException(string.Concat(strArrays)); } }
/// <summary> /// Gets the length in septets of the specified text. /// </summary> /// <param name="text">The text the get the length for.</param> /// <returns>The text length.</returns> public static int GetTextLength(string text) { return(TextDataConverter.StringTo7Bit(text).Length); }
/// <summary> /// Converts a string consisting of characters from the ISO-8859-1 /// character set into a string of corresponding characters of the /// "GSM 7-bit default alphabet" character set. /// </summary> /// <param name="s">The string to convert.</param> /// <returns>The converted string.</returns> /// <remarks> /// <para>Throws an exception when an invalid character is encountered in the string.</para> /// <para>Note that the converted string does not need to have the same /// length as the original one because some characters may be escaped.</para> /// </remarks> /// <exception cref="T:System.ArgumentException">An invalid character is encountered in the string.</exception> public static string StringTo7Bit(string s) { bool flag = false; return(TextDataConverter.StringTo7Bit(s, true, out flag)); }
/// <summary> /// Converts a string consisting of characters from the ISO-8859-1 /// character set into a string of corresponding characters of the /// "GSM 7-bit default alphabet" character set. /// </summary> /// <param name="s">The string to convert.</param> /// <param name="throwExceptions">If true, throws an exception if /// an invalid character is encountered. If false, replaces every /// unknown character with a question mark (?).</param> /// <param name="charsReplaced">Will be set to true if invalid characters /// were replaced. <b>throwExceptions</b> must be false for this to work.</param> /// <returns>The converted string.</returns> /// <remarks> /// Note that the converted string does not need to have the same /// length as the original one because some characters may be escaped. /// </remarks> /// <exception cref="T:System.ArgumentException">throwExceptions is true and invalid character is encountered in the string.</exception> public static string StringTo7Bit(string s, bool throwExceptions, out bool charsReplaced) { StringBuilder stringBuilder = new StringBuilder(); charsReplaced = false; bool flag = false; int i = 0; int num = 0; for (i = 0; i < s.Length; i++) { flag = false; char chr = s.Substring(i, 1)[0]; num = 0; while (num <= TextDataConverter.sevenBitDefault.GetUpperBound(0)) { if (TextDataConverter.sevenBitDefault[num] != chr) { num++; } else { stringBuilder.Append((char)num); flag = true; break; } } if (!flag) { try { byte sevenBitExtension = TextDataConverter.CharToSevenBitExtension(chr); stringBuilder.Append('\u001B'); stringBuilder.Append(sevenBitExtension); flag = true; } catch (Exception exception) { } } if (!flag) { if (!throwExceptions) { stringBuilder.Append('?'); charsReplaced = true; } else { object[] str = new object[5]; str[0] = "The character '"; str[1] = chr; str[2] = "' at position "; int num1 = i + 1; str[3] = num1.ToString(); str[4] = " does not exist in the GSM 7-bit default alphabet."; throw new ArgumentException(string.Concat(str)); } } } return(stringBuilder.ToString()); }
/// <summary> /// Converts a string consisting of characters from the GSM /// "7-bit default alphabet" into a string of corresponding characters /// of the ISO-8859-1 character set. /// </summary> /// <param name="s">The string to convert.</param> /// <param name="throwExceptions">If true, throws an exception if /// an invalid character is encountered. If false, replaces every /// unknown character with a question mark (?).</param> /// <returns>The converted string.</returns> /// <remarks> /// <para>Note that the converted string does not necessarily have the same /// length as the original one because some characters may be escaped.</para> /// <para>This method throws an exception if an invalid character /// is encountered.</para> /// </remarks> public static string SevenBitToString(string s, bool throwExceptions) { string empty = string.Empty; bool flag = false; for (int i = 0; i < s.Length; i++) { byte num = (byte)s[i]; if (!flag) { if (num == 27) { flag = true; } else { if (num > TextDataConverter.sevenBitDefault.GetUpperBound(0)) { if (!throwExceptions) { empty = string.Concat(empty, (char)63); } else { object[] str = new object[5]; str[0] = "Character '"; str[1] = (char)num; str[2] = "' at position "; int num1 = i + 1; str[3] = num1.ToString(); str[4] = " is not part of the GSM 7-bit default alphabet."; throw new ArgumentException(string.Concat(str)); } } else { empty = string.Concat(empty, TextDataConverter.sevenBitDefault[num]); } } } else { try { empty = string.Concat(empty, TextDataConverter.SevenBitExtensionToChar(num)); } catch (Exception exception) { if (!throwExceptions) { empty = string.Concat(empty, (char)63); } else { throw; } } flag = false; } } return(empty); }
/// <summary> /// Converts a string consisting of characters from the GSM /// "7-bit default alphabet" into a string of corresponding characters /// of the ISO-8859-1 character set. /// </summary> /// <param name="s">The string to convert.</param> /// <returns>The converted string.</returns> /// <remarks> /// <para>Note that the converted string does not necessarily have the same /// length as the original one because some characters may be escaped.</para> /// <para>This method throws an exception if an invalid character /// is encountered.</para> /// </remarks> public static string SevenBitToString(string s) { return(TextDataConverter.SevenBitToString(s, true)); }
/// <summary> /// Decodes the text from 7-Bit user data. /// </summary> /// <param name="userData">The user data to decode. Must contain an encoded GSM 7-Bit default text packed into octets.</param> /// <returns>The decoded user data.</returns> public static string Decode7BitText(byte[] userData) { string septetsStr = TextDataConverter.OctetsToSeptetsStr(userData); return(TextDataConverter.SevenBitToString(septetsStr, true)); }