ByteArray - UrlEncodeToBytes
Converts an array of bytes into a URL-encoded array of bytes.
public static void Main() { string fileName = "test.txt"; string urlString = "This a simple text."; //C# Extension Method: String - ToByteArray var bytes = urlString.ToByteArray(); //C# Extension Method: ByteArray - UrlEncodeToBytes var output = bytes.UrlEncodeToBytes(); fileName.ToFileInfo().WriteAllBytes(output); Console.WriteLine(fileName.ToFileInfo().ReadAllText()); }
View Source
using System; #if !NETSTANDARD using System.Web; #endif public static partial class Extensions { #if !NETSTANDARD /// <summary> /// Converts an array of bytes into a URL-encoded array of bytes. /// </summary> /// <param name="bytes">The array of bytes to encode.</param> /// <returns>An encoded array of bytes.</returns> public static Byte[] UrlEncodeToBytes(this Byte[] bytes) { return HttpUtility.UrlEncodeToBytes(bytes); } /// <summary> /// Converts an array of bytes into a URL-encoded array of bytes, starting at the specified position in the array /// and continuing for the specified number of bytes. /// </summary> /// <param name="bytes">The array of bytes to encode.</param> /// <param name="offset">The position in the byte array at which to begin encoding.</param> /// <param name="count">The number of bytes to encode.</param> /// <returns>An encoded array of bytes.</returns> public static Byte[] UrlEncodeToBytes(this Byte[] bytes, Int32 offset, Int32 count) { return HttpUtility.UrlEncodeToBytes(bytes, offset, count); } #endif }