/// <summary> /// Converts a string to a byte array /// </summary> /// <param name="Input">input string</param> /// <param name="EncodingUsing">The type of encoding the string is using (defaults to UTF8)</param> /// <returns>the byte array representing the string</returns> public static byte[] ToByteArray(this string Input, Encoding EncodingUsing = null) { return string.IsNullOrEmpty(Input) ? new byte[0] : EncodingUsing.Check(new UTF8Encoding()).GetBytes(Input); }
/// <summary> /// Converts from the specified encoding to a base 64 string /// </summary> /// <param name="Input">Input string</param> /// <param name="Options">Base 64 formatting options</param> /// <param name="OriginalEncodingUsing"> /// The type of encoding the string is using (defaults to UTF8) /// </param> /// <returns>Bas64 string</returns> public static string ToString(this string Input, Base64FormattingOptions Options, Encoding OriginalEncodingUsing = null) { if (string.IsNullOrEmpty(Input)) return ""; byte[] TempArray = OriginalEncodingUsing.Check(new UTF8Encoding()).GetBytes(Input); return Convert.ToBase64String(TempArray, Options); }
/// <summary> /// Converts base 64 string based on the encoding passed in /// </summary> /// <param name="Input">Input string</param> /// <param name="EncodingUsing">The type of encoding the string is using (defaults to UTF8)</param> /// <returns>string in the encoding format</returns> public static string FromBase64(this string Input, Encoding EncodingUsing) { if (string.IsNullOrEmpty(Input)) return ""; byte[] TempArray = Convert.FromBase64String(Input); return EncodingUsing.Check(() => new UTF8Encoding()).GetString(TempArray); }
/// <summary> /// Converts a string to a string of another encoding /// </summary> /// <param name="Input">input string</param> /// <param name="OriginalEncodingUsing"> /// The type of encoding the string is currently using (defaults to ASCII) /// </param> /// <param name="EncodingUsing"> /// The type of encoding the string is converted into (defaults to UTF8) /// </param> /// <returns>string of the byte array</returns> public static string Encode(this string Input, Encoding OriginalEncodingUsing = null, Encoding EncodingUsing = null) { if (string.IsNullOrEmpty(Input)) return ""; OriginalEncodingUsing = OriginalEncodingUsing.Check(new ASCIIEncoding()); EncodingUsing = EncodingUsing.Check(new UTF8Encoding()); return Encoding.Convert(OriginalEncodingUsing, EncodingUsing, Input.ToByteArray(OriginalEncodingUsing)) .ToString(EncodingUsing); }
/// <summary> /// Converts a byte array to a string /// </summary> /// <param name="Input">input array</param> /// <param name="EncodingUsing"> /// The type of encoding the string is using (defaults to UTF8) /// </param> /// <param name="Count"> /// Number of bytes starting at the index to convert (use -1 for the entire array starting /// at the index) /// </param> /// <param name="Index">Index to start at</param> /// <returns>string of the byte array</returns> public static string ToString(this byte[] Input, Encoding EncodingUsing, int Index = 0, int Count = -1) { if (Input == null) return ""; if (Count == -1) Count = Input.Length - Index; return EncodingUsing.Check(new UTF8Encoding()).GetString(Input, Index, Count); }
/// <summary> /// Saves a string to a file (asynchronously) /// </summary> /// <param name="File">File to save to</param> /// <param name="Content">Content to save to the file</param> /// <param name="CallBack">Call back function</param> /// <param name="StateObject">State object</param> /// <param name="EncodingUsing">Encoding that the content is using (defaults to ASCII)</param> /// <param name="Mode">Mode for saving the file (defaults to Create)</param> /// <returns>The FileInfo object</returns> public static FileInfo SaveAsync(this FileInfo File, string Content, AsyncCallback CallBack, object StateObject, FileMode Mode = FileMode.Create, Encoding EncodingUsing = null) { Contract.Requires<ArgumentNullException>(File != null, "File"); return File.SaveAsync(EncodingUsing.Check(new ASCIIEncoding()).GetBytes(Content), CallBack, StateObject, Mode); }
/// <summary> /// Saves a string to a file /// </summary> /// <param name="File">File to save to</param> /// <param name="Content">Content to save to the file</param> /// <param name="EncodingUsing">Encoding that the content is using (defaults to ASCII)</param> /// <param name="Mode">Mode for saving the file (defaults to Create)</param> /// <returns>The FileInfo object</returns> public static FileInfo Save(this FileInfo File, string Content, FileMode Mode = FileMode.Create, Encoding EncodingUsing = null) { Contract.Requires<ArgumentNullException>(File != null, "File"); return File.Save(EncodingUsing.Check(new ASCIIEncoding()).GetBytes(Content), Mode); }