/// <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 ""; var TempArray = OriginalEncodingUsing.Check(new UTF8Encoding()).GetBytes(Input); return Convert.ToBase64String(TempArray, Options); }
/// <summary> /// Create a Base64String from the Surface by saving it to a memory stream and converting it. /// Should be avoided if possible, as this uses a lot of memory. /// </summary> /// <returns>string</returns> public string ToBase64String(Base64FormattingOptions formattingOptions) { using (MemoryStream stream = new MemoryStream()) { ImageOutput.SaveToStream(_surface, stream, _outputSettings); return(Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions)); } }
/// <summary> /// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits. Parameters specify the subset as an offset in the input array, the number of elements in the array to convert, and whether to insert line breaks in the return value. /// </summary> /// <param name="inArray">An array of 8-bit unsigned integers. </param> /// <param name="offset">An offset in <paramref name="inArray"/>.</param> /// <param name="length">The number of elements of <paramref name="inArray"/> to convert.</param> /// <param name="options">cref="System.InsertLineBreaks" to insert a line break every 76 characters, or None to not insert line breaks.</param> /// <returns>The string representation in base 64 of <paramref name="length"/> elements of <paramref name="inArray"/>, starting at position <paramref name="offset"/>.</returns> public static string ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) { //Do data verfication #pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one if (inArray == null) { throw new ArgumentNullException(); } #pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one #pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one if (length < 0) { throw new ArgumentOutOfRangeException(); } #pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one #pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one if (offset < 0) { throw new ArgumentOutOfRangeException(); } #pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one #pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one if (options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) { throw new ArgumentException(); } #pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one return(ToBase64String(inArray, offset, length, options == Base64FormattingOptions.InsertLineBreaks)); }
public static Span <char> ToBase64Chars(ReadOnlySpan <byte> bytes, Span <char> chars, Base64FormattingOptions options = Base64FormattingOptions.None) { if (options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) { throw new ArgumentException(string.Format(_illegalEnumValue, (int)options), nameof(options)); } if (bytes.Length == 0) { return(Span <char> .Empty); } var charLengthRequired = ToBase64_CalculateAndValidateOutputLength(bytes.Length, options); if (charLengthRequired > chars.Length) { throw new ArgumentException("insufficient memory", nameof(bytes)); // TODO: Exception message } var charsWritten = ConvertToBase64Array(chars, bytes, 0, bytes.Length, options); return(chars.Slice(start: 0, length: charsWritten)); }
public static string ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) { var s64 = Convert.ToBase64String(inArray, offset, length); if (options == Base64FormattingOptions.None) { return(s64); } length = s64.Length; var sb = new StringBuilder(); int remainder = length % MaxBytesPerLine; int full = length / MaxBytesPerLine; offset = 0; for (int i = 0; i < full; i++) { sb.AppendLine(s64.Substring(offset, MaxBytesPerLine)); offset += MaxBytesPerLine; } // we never complete (i.e. the last line) with a new line if (remainder == 0) { int nll = CrestronEnvironment.NewLine.Length; sb.Remove(sb.Length - nll, nll); } else { sb.Append(s64.Substring(offset)); } return(sb.ToString()); }
/// <summary> /// Create a Base64String from the image by saving it to a memory stream and converting it. /// Should be avoided if possible, as this uses a lot of memory. /// </summary> /// <returns>string</returns> public string ToBase64String(Base64FormattingOptions formattingOptions) { using (MemoryStream stream = new MemoryStream()) { ImageOutput.SaveToStream(bitmap, null, stream, outputSettings); return(System.Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions)); } }
private static int ConvertToBase64Array( Span <char> chars, ReadOnlySpan <byte> bytes, int offset, int length, Base64FormattingOptions options) { var insertLineBreaks = (options & Base64FormattingOptions.InsertLineBreaks) != 0; var lengthmod3 = length % 3; var calcLength = offset + (length - lengthmod3); var j = 0; var charcount = 0; //Convert three bytes at a time to base64 notation. This will consume 4 chars. int i; for (i = offset; i < calcLength; i += 3) { if (insertLineBreaks) { if (charcount == _base64LineBreakPosition) { chars[j++] = '\r'; chars[j++] = '\n'; charcount = 0; } charcount += 4; } chars[j] = _base64Table[(bytes[i] & 0xfc) >> 2]; chars[j + 1] = _base64Table[((bytes[i] & 0x03) << 4) | ((bytes[i + 1] & 0xf0) >> 4)]; chars[j + 2] = _base64Table[((bytes[i + 1] & 0x0f) << 2) | ((bytes[i + 2] & 0xc0) >> 6)]; chars[j + 3] = _base64Table[bytes[i + 2] & 0x3f]; j += 4; } //Where we left off before i = calcLength; if (insertLineBreaks && lengthmod3 != 0 && charcount == _base64LineBreakPosition) { chars[j++] = '\r'; chars[j++] = '\n'; } switch (lengthmod3) { case 2: //One character padding needed chars[j] = _base64Table[(bytes[i] & 0xfc) >> 2]; chars[j + 1] = _base64Table[((bytes[i] & 0x03) << 4) | ((bytes[i + 1] & 0xf0) >> 4)]; chars[j + 2] = _base64Table[(bytes[i + 1] & 0x0f) << 2]; chars[j + 3] = _base64Table[64]; //Pad j += 4; break; case 1: // Two character padding needed chars[j] = _base64Table[(bytes[i] & 0xfc) >> 2]; chars[j + 1] = _base64Table[(bytes[i] & 0x03) << 4]; chars[j + 2] = _base64Table[64]; //Pad chars[j + 3] = _base64Table[64]; //Pad j += 4; break; } return(j); }
public static bool TryToBase64Chars(ReadOnlySpan <byte> bytes, Span <char> chars, out int charsWritten, Base64FormattingOptions options = Base64FormattingOptions.None) { if (options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) { throw new ArgumentException(string.Format(_illegalEnumValue, (int)options), nameof(options)); } if (bytes.Length == 0) { charsWritten = 0; return(true); } var charLengthRequired = ToBase64_CalculateAndValidateOutputLength(bytes.Length, options); if (charLengthRequired > chars.Length) { charsWritten = 0; return(false); } charsWritten = ConvertToBase64Array(chars, bytes, 0, bytes.Length, options); return(true); }
/// <summary> /// Encrypt a given string using specific password and salt values /// </summary> /// <param name="value">string to encrypt</param> /// <param name="password"></param> /// <param name="salt"></param> /// <param name="base64Options">whether or not line breaks are added to the resulting Base64 result.</param> /// <returns>string containing protected content</returns> public string Encrypt(string value, string password, string salt, Base64FormattingOptions base64Options) { if (string.IsNullOrEmpty(value)) { return(string.Empty); } if (string.IsNullOrEmpty(password)) { throw new ArgumentException("password can not be blank"); } if (string.IsNullOrEmpty(salt)) { throw new ArgumentException("salt can not be blank"); } DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt)); byte[] rgbKey = rgb.GetBytes(this.CryptoAlgorithm.KeySize >> 3); byte[] rgbIV = rgb.GetBytes(this.CryptoAlgorithm.BlockSize >> 3); ICryptoTransform transform = this.CryptoAlgorithm.CreateEncryptor(rgbKey, rgbIV); using (MemoryStream buffer = new MemoryStream()) { using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write)) { using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode)) { writer.Write(value); } } return(Convert.ToBase64String(buffer.ToArray(), base64Options)); } }
public string ConvertToString(int startIndex, bool createBase64String, Base64FormattingOptions base64FormattingOption = Base64FormattingOptions.None) { var retVal = createBase64String ? Convert.ToBase64String(_lastComputedHash, startIndex, _lastComputedHash.Length, base64FormattingOption) : BitConverter.ToString(_lastComputedHash); OnStringGenerated(retVal); return(retVal); }
public static String ToBase64(this Object input, Base64FormattingOptions base64FormattingOptions = Base64FormattingOptions.None) { if (input == null) { return(String.Empty); } Type type = input.GetType(); // typeof(TObject); // Add support for Nullable types if (type.IsNullable()) { type = type.GetGenericArguments()[0]; } MethodInfo methodInfo; // Throw in a little static reflection caching if (!ParserUtilities._byteArrayMap.TryGetValue(type, out methodInfo)) { // Attempt to find inbuilt ToByteArray methods methodInfo = type.GetMethod("ToByteArray", new Type[] { }); // Null the mapping if the return type is wrong if ((methodInfo == null) || (methodInfo.ReturnType != typeof(Byte[]))) { ParserUtilities._byteArrayMap[type] = null; } else { ParserUtilities._byteArrayMap[type] = methodInfo; } } // Then use the inbuilt methods if (methodInfo != null) { return(Convert.ToBase64String((Byte[])methodInfo.Invoke(input, null), base64FormattingOptions)); } // Throw in a little static reflection caching if (!ParserUtilities._parserMap.TryGetValue(type, out methodInfo)) { // Attempt to find inbuilt parsing methods methodInfo = type.GetMethod("Parse", new Type[] { typeof(String) }); ParserUtilities._parserMap[type] = methodInfo; } // If there's inbuilt parsing methods, assume there is a useful ToString method if (methodInfo != null) { return(Convert.ToBase64String(Encoding.UTF8.GetBytes(input.ToString()), base64FormattingOptions)); } return(BinaryUtilities.ToBinary(input, base64FormattingOptions)); }
/// <summary> /// Returns the base64-encoded representation of the string /// </summary> /// <param name="options"></param> /// <returns>A new string representing the base64-encoded value of the string</returns> public static string ToBase64String(this string value, Base64FormattingOptions options = Base64FormattingOptions.None) { if (string.IsNullOrEmpty(value)) { return(value); } return(Convert.ToBase64String(Encoding.ASCII.GetBytes(value), options)); }
/// <summary> /// 字符串转换为Base64字符串 /// </summary> /// <param name="bytes">字节数组</param> /// <param name="base64FormattingOptions">base64格式</param> /// <param name="startIndex">起始索引</param> /// <param name="length">字节长度</param> /// <returns>Base64字符串</returns> public static string ToBase64String(this byte[] bytes, Base64FormattingOptions base64FormattingOptions = Base64FormattingOptions.None, int?startIndex = null, int?length = null) { return(bytes == null ? null : Convert.ToBase64String(GetRealBytes(bytes, startIndex, length), base64FormattingOptions)); }
public string ToBase64(byte[] content, int offset = 0, int?length = null, Base64FormattingOptions formattingOptions = Base64FormattingOptions.None) { if (!length.HasValue) { length = content.Length - offset; } return(Convert.ToBase64String(content, offset, length.Value, formattingOptions)); }
/// <summary> /// Converts a byte array into a base 64 string /// </summary> /// <param name="Input">Input array</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> /// <param name="Options">Base 64 formatting options</param> /// <returns>The equivalent byte array in a base 64 string</returns> public static string ToString(this byte[] Input, Base64FormattingOptions Options, int Index = 0, int Count = -1) { if (Count == -1) { Count = Input.Length - Index; } return(Input == null ? "" : Convert.ToBase64String(Input, Index, Count, Options)); }
/// <summary> /// <see cref="Byte"/> 배열을 Base64 인코딩으로 변환합니다. /// </summary> /// <param name="bytes">><see cref="Byte"/> 배열입니다.</param> /// <param name="option">인코딩 옵션을 <see cref="Base64FormattingOptions"/> 객체에 설정합니다.</param> /// <returns>Base64 인코딩된 문자열 입니다.</returns> public static string ToBase64(this byte[] bytes, Base64FormattingOptions option) { if (bytes == null) { return(null); } return(Convert.ToBase64String(bytes, option)); }
public GUI() { InitializeComponent(); modeIndex = 0; comboBoxMode.SelectedIndex = modeIndex; openFileDialogSource.Filter = "All files (*.*)|*.*"; comboBoxFormat.SelectedIndex = 0; format = Base64FormattingOptions.InsertLineBreaks; }
/// <summary> /// Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits. A parameter specifies whether to insert line breaks in the return value. /// </summary> /// <param name="inArray">An array of 8-bit unsigned integers.</param> /// <param name="options"><see cref="Base64FormattingOptions.InsertLineBreaks"/> to insert a line break every 76 characters, or None to not insert line breaks.</param> /// <returns>The string representation in base 64 of the elements in <paramref name="inArray"/>.</returns> public static String ToBase64String(byte[] inArray, Base64FormattingOptions options) { if (inArray == null) { throw new ArgumentNullException(); } return(ToBase64String(inArray, 0, inArray.Length, options)); }
private HashUtilityConfig setOptions(AlgorithmOptions algorithmOption, EncodingOptions encodingOption, string hashName, Base64FormattingOptions base64FormattingOption, bool useBase64Encoding) { AlgorithmOption = algorithmOption; EncodingOption = encodingOption; HashName = hashName; Base64FormattingOption = base64FormattingOption; UseBase64Encoding = useBase64Encoding; return(this); }
/// <summary> /// Converts a byte array into a base 64 string /// </summary> /// <param name="Input">Input array</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> /// <param name="Options">Base 64 formatting options</param> /// <returns>The equivalent byte array in a base 64 string</returns> public static string ToString(this byte[] Input, Base64FormattingOptions Options, int Index = 0, int Count = -1) { Contract.Requires <ArgumentException>(Index >= 0, "Index"); if (Count == -1) { Count = Input.Length - Index; } return(Input == null ? "" : Convert.ToBase64String(Input, Index, Count, Options)); }
private void WriteByteList(List <byte> bytes, Base64FormattingOptions options) { byte[] array = bytes.ToArray(); bytes = null; string base64 = Convert.ToBase64String(array, options); array = null; WriteObject(base64); }
/// <summary> /// Converts the current byte array to an equivalent array of a Unicode characters /// encoded with base-64 digits. A parameter specify whether line breaks are inserted in /// the output array. /// </summary> /// <param name="me">The current byte array.</param> /// <param name="options"> /// <see cref="Base64FormattingOptions.InsertLineBreaks"/> to insert a line break every /// 76 characters, or <see cref="Base64FormattingOptions.None"/> to not insert line breaks. /// </param> /// <returns> /// A character array of Unicode characters that's equivalent to the current byte array /// encoded with base-64 digits. /// </returns> public static char[] ToBase64CharArray(this byte[] me, Base64FormattingOptions options) { int length = me.Length; char[] chars = new char[CalculateBase64Count(length)]; Convert.ToBase64CharArray(me, 0, length, chars, 0, options); return(chars); }
/// <summary> /// Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits. A parameter specifies whether to insert line breaks in the return value. /// </summary> /// <param name="inArray">An array of 8-bit unsigned integers.</param> /// <param name="options"><see cref="Base64FormattingOptions.InsertLineBreaks"/> to insert a line break every 76 characters, or None to not insert line breaks.</param> /// <returns>The string representation in base 64 of the elements in <paramref name="inArray"/>.</returns> public static String ToBase64String(byte[] inArray, Base64FormattingOptions options) { #pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one if (inArray == null) { throw new ArgumentNullException(); } #pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one return(ToBase64String(inArray, 0, inArray.Length, options)); }
/// <summary> /// Converts the <c>byte[]</c> parameter to base64 string. /// </summary> /// <param name="array">The array to be converted.</param> /// <param name="options">The base64 conversion options.</param> /// <returns>The base64 representation of the array.</returns> /// <remarks> /// Note that the returned string contains only the following characters: a-z, A-Z, 0-9, +, / and =. /// If stored, for saving space, consider storing the result as an ASCII string instead of Unicode string. /// </remarks> public static string ToBase64String( this byte[] array, Base64FormattingOptions options = Base64FormattingOptions.None) { if (array == null) { throw new ArgumentNullException(nameof(array)); } return(Convert.ToBase64String(array, options)); }
/// <summary> /// 为指定字符串使用Base64编码加密 /// </summary> /// <param name="eText">要加密的字符串</param> /// <param name="IsBranch">如果为true,编码时每76个字符中插入分行符</param> /// <returns></returns> public static string EncrypBase64(string eText, bool IsBranch) { if (string.IsNullOrEmpty(eText)) { throw new Exception("要加密的字符串不能为空!"); } byte[] Arr = Encoding.Default.GetBytes(eText.Replace("+", "%2B")); Base64FormattingOptions option = IsBranch ? Base64FormattingOptions.InsertLineBreaks : Base64FormattingOptions.None; return(Convert.ToBase64String(Arr, option)); }
private void comboBoxFormat_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBoxFormat.SelectedIndex) { case 0: format = Base64FormattingOptions.InsertLineBreaks; break; case 1: format = Base64FormattingOptions.None; break; } }
public static string GetString(Stream ms, Base64FormattingOptions breaks) { byte[] mem = new byte[ms.Length]; long pos = ms.Position; ms.Seek(0, SeekOrigin.Begin); BinaryReader joe = new BinaryReader(ms); joe.Read(mem, 0, (int)ms.Length); string sam = Convert.ToBase64String(mem, breaks); ms.Seek(pos, SeekOrigin.Begin); joe = null; mem = null; return(sam); }
} /*}}}*/ public static string base64_encode(string rawData, Encoding rawEnc, Base64FormattingOptions option) /*{{{ */ { try { byte[] bData = rawEnc.GetBytes(rawData); return(Convert.ToBase64String(bData, option)); } catch { return(""); } } /*}}}*/
/// <summary> /// Base64编码(ReadOnlySpan<Byte> → String) /// </summary> /// <param name="inArray"></param> /// <param name="options"></param> /// <returns></returns> public static string Base64Encode(this ReadOnlySpan <byte> inArray, Base64FormattingOptions options = Base64FormattingOptions.None) { switch (options) { case Base64FormattingOptions.InsertLineBreaks: return(Convert.ToBase64String(inArray, Base64FormattingOptions.InsertLineBreaks));; case Base64FormattingOptions.None: return(Base64.Default.Encode(inArray)); default: throw new ArgumentOutOfRangeException(nameof(options)); } }
private static object BuildExpectedString(Base64FormattingOptions insertLineBreaks) { if (insertLineBreaks != Base64FormattingOptions.InsertLineBreaks) { return(base64TestEncode); } // get line break count (line break every 76 chars) int lineBreakCount = base64TestEncode.Length / 76; StringBuilder st = new StringBuilder(); int outputLength = base64TestEncode.Length; int offset = 0; for (int i = 0; i <= lineBreakCount; i++) { // how many chars to copy int count; if (outputLength > 76) { // first/next 76 chars count = 76; } else { // last outputLength chars count = outputLength; } // copy first/next count chars // because we are using same offset for both arrays, we need to discount line break for tmp array st.Append(base64TestEncode, offset, count); // move offset for next copy offset += count; // remove copied chars from original output length if more than 76 chars still to be copied if (outputLength >= 76) { // more chars // adjust output length outputLength -= 76; // add line break st.AppendLine(); } } return(st.ToString()); }
public virtual void GetStandardValues() { LexographicEnumConverter TestSubject = new LexographicEnumConverter(typeof(Base64FormattingOptions)); Base64FormattingOptions[] expected = new Base64FormattingOptions[] { Base64FormattingOptions.InsertLineBreaks, Base64FormattingOptions.None }; Base64FormattingOptions[] actual = new Base64FormattingOptions[2]; TestSubject.GetStandardValues().CopyTo(actual,0); for (int i = 0; i < expected.Length; i++) { Assert.AreEqual(expected[i], actual[i]); } }
/// <summary>Converts the specified array to a Base64 string.</summary> /// <param name="value">The array to convert.</param> /// <param name="options">The <see cref="T:Base64FormattingOptions"/> to use.</param> /// <returns>The converted string.</returns> public static string ToDataString(this byte[] value, Base64FormattingOptions options) { return value.HasItems() ? Convert.ToBase64String(value, options) : String.Empty; }
/// <summary> /// Converts a byte array into a base 64 string /// </summary> /// <param name="Input">Input array</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> /// <param name="Options">Base 64 formatting options</param> /// <returns>The equivalent byte array in a base 64 string</returns> public static string ToString(this byte[] Input, Base64FormattingOptions Options, int Index = 0, int Count = -1) { Contract.Requires<ArgumentException>(Index >= 0, "Index"); if (Count == -1) Count = Input.Length - Index; return Input == null ? "" : Convert.ToBase64String(Input, Index, Count, Options); }
/// <summary> /// Create a Base64String from the Surface by saving it to a memory stream and converting it. /// Should be avoided if possible, as this uses a lot of memory. /// </summary> /// <returns>string</returns> public string ToBase64String(Base64FormattingOptions formattingOptions) { using (MemoryStream stream = new MemoryStream()) { ImageOutput.SaveToStream(_surface, stream, _outputSettings); return Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions); } }
/// <summary> /// Converts the route to IOF XML 3.0 binary format and returns it as a base64-encoded string. /// </summary> /// <param name="formattingOptions">The formatting options for the base64-encoded string.</param> public string ToBase64String(Base64FormattingOptions formattingOptions = Base64FormattingOptions.None) { return Convert.ToBase64String(ToByteArray(), formattingOptions); }
static string ToBase64 (int len, Base64FormattingOptions options) { return Convert.ToBase64String (new byte [len], options); }
public unsafe static string ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) { if (inArray == null) { throw new ArgumentNullException("inArray"); } if (length < 0) { throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive")); } if (options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) { throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", new object[] { (int)options })); } int num = inArray.Length; if (offset > num - length) { throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength")); } if (num == 0) { return string.Empty; } bool insertLineBreaks = options == Base64FormattingOptions.InsertLineBreaks; int length2 = Convert.CalculateOutputLength(length, insertLineBreaks); string text = string.FastAllocateString(length2); fixed (char* outChars = text) { fixed (byte* ptr = inArray) { Convert.ConvertToBase64Array(outChars, ptr, offset, length, insertLineBreaks); return text; } } }
public static unsafe String ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) { //Do data verfication if (inArray==null) throw new ArgumentNullException("inArray"); if (length<0) throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_Index")); if (offset<0) throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive")); if (options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)options)); Contract.Ensures(Contract.Result<string>() != null); Contract.EndContractBlock(); int inArrayLength; int stringLength; inArrayLength = inArray.Length; if (offset > (inArrayLength - length)) throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength")); if (inArrayLength == 0) return String.Empty; bool insertLineBreaks = (options == Base64FormattingOptions.InsertLineBreaks); //Create the new string. This is the maximally required length. stringLength = ToBase64_CalculateAndValidateOutputLength(length, insertLineBreaks); string returnString = string.FastAllocateString(stringLength); fixed (char* outChars = returnString){ fixed (byte* inData = inArray) { int j = ConvertToBase64Array(outChars,inData,offset,length, insertLineBreaks); BCLDebug.Assert(returnString.Length == j, "returnString.Length == j"); return returnString; } } }
public static unsafe string ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) { if (inArray == null) { throw new ArgumentNullException("inArray"); } if (length < 0) { throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive")); } int num = inArray.Length; if (offset > (num - length)) { throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength")); } if ((options < Base64FormattingOptions.None) || (options > Base64FormattingOptions.InsertLineBreaks)) { throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", new object[] { (int) options })); } if (num == 0) { return string.Empty; } bool insertLineBreaks = options == Base64FormattingOptions.InsertLineBreaks; int capacity = CalculateOutputLength(length, insertLineBreaks); string stringForStringBuilder = string.GetStringForStringBuilder(string.Empty, capacity); fixed (char* str3 = ((char*) stringForStringBuilder)) { char* outChars = str3; fixed (byte* numRef = inArray) { int newLength = ConvertToBase64Array(outChars, numRef, offset, length, insertLineBreaks); stringForStringBuilder.SetLength(newLength); return stringForStringBuilder; } } }
public static string ToBase64String(byte[] inArray, Base64FormattingOptions options) {}
public static String Encode(IEnumerable<Byte> data, Base64FormattingOptions options) { return Convert.ToBase64String(data.ToArray(), options); }
public static String Base64Encode(this IEnumerable<Byte> data, Base64FormattingOptions options) { return Encode(data, options); }
public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options) { if (outArray == null) throw new ArgumentNullException("outArray"); if (offsetOut < 0) throw new ArgumentOutOfRangeException("offsetOut"); var encoded = ToBase64String(inArray, offsetIn, length); encoded.CopyTo(0, outArray, offsetOut, encoded.Length); return encoded.Length; }
/// <summary> /// Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with /// base-64 digits. A parameter specifies whether to insert line breaks in the return value. /// </summary> /// <param name="inArray">An array of 8-bit unsigned integers.</param> /// <param name="options">to insert a line break every 76 characters, or to not insert line breaks.</param> /// <returns>The string representation in base 64 of the elements in .</returns> public static String ToBase64String(this Byte[] inArray, Base64FormattingOptions options) { return Convert.ToBase64String(inArray, options); }
/// <summary> /// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is /// encoded with base-64 digits. Parameters specify the subset as an offset in the input array, the number of /// elements in the array to convert, and whether to insert line breaks in the return value. /// </summary> /// <param name="inArray">An array of 8-bit unsigned integers.</param> /// <param name="offset">An offset in .</param> /// <param name="length">The number of elements of to convert.</param> /// <param name="options">to insert a line break every 76 characters, or to not insert line breaks.</param> /// <returns>The string representation in base 64 of elements of , starting at position .</returns> public static String ToBase64String(this Byte[] inArray, Int32 offset, Int32 length, Base64FormattingOptions options) { return Convert.ToBase64String(inArray, offset, length, options); }
public static string ToBase64String (byte[] inArray, int offset, int length, Base64FormattingOptions options) { if (inArray == null) throw new ArgumentNullException ("inArray"); if (offset < 0 || length < 0) throw new ArgumentOutOfRangeException ("offset < 0 || length < 0"); // avoid integer overflow if (offset > inArray.Length - length) throw new ArgumentOutOfRangeException ("offset + length > array.Length"); if (length == 0) return String.Empty; if (options == Base64FormattingOptions.InsertLineBreaks) return ToBase64StringBuilderWithLine (inArray, offset, length).ToString (); else return Encoding.ASCII.GetString (Base64Helper.TransformFinalBlock (inArray, offset, length)); }
public static String ToBase64String(byte[] inArray, Base64FormattingOptions options) { if (inArray==null) { throw new ArgumentNullException("inArray"); } Contract.Ensures(Contract.Result<string>() != null); Contract.EndContractBlock(); return ToBase64String(inArray, 0, inArray.Length, options); }
public static string ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) { if (inArray == null) throw new ArgumentNullException("inArray"); if (offset < 0 || length < 0) throw new ArgumentOutOfRangeException("offset < 0 || length < 0"); // avoid integer overflow if (offset > inArray.Length - length) throw new ArgumentOutOfRangeException("offset + length > array.Length"); if (length == 0) return String.Empty; #if ANDROID_8P return Base64.EncodeToString(inArray, offset, length, Base64.DEFAULT); #else throw new NotImplementedException("System.Convert.ToBase64String"); #endif }
public static unsafe int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options) { //Do data verfication if (inArray==null) throw new ArgumentNullException("inArray"); if (outArray==null) throw new ArgumentNullException("outArray"); if (length<0) throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_Index")); if (offsetIn<0) throw new ArgumentOutOfRangeException("offsetIn", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive")); if (offsetOut<0) throw new ArgumentOutOfRangeException("offsetOut", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive")); if( options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) { throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)options)); } Contract.Ensures(Contract.Result<int>() >= 0); Contract.Ensures(Contract.Result<int>() <= outArray.Length); Contract.EndContractBlock(); int retVal; int inArrayLength; int outArrayLength; int numElementsToCopy; inArrayLength = inArray.Length; if (offsetIn > (int)(inArrayLength - length)) throw new ArgumentOutOfRangeException("offsetIn", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength")); if (inArrayLength == 0) return 0; bool insertLineBreaks = (options == Base64FormattingOptions.InsertLineBreaks); //This is the maximally required length that must be available in the char array outArrayLength = outArray.Length; // Length of the char buffer required numElementsToCopy = ToBase64_CalculateAndValidateOutputLength(length, insertLineBreaks); if (offsetOut > (int)(outArrayLength - numElementsToCopy)) throw new ArgumentOutOfRangeException("offsetOut", Environment.GetResourceString("ArgumentOutOfRange_OffsetOut")); fixed (char* outChars = &outArray[offsetOut]) { fixed (byte* inData = inArray) { retVal = ConvertToBase64Array(outChars,inData,offsetIn,length, insertLineBreaks); } } return retVal; }
public static unsafe String ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) { int inArrayLength; int stringLength; //Do data verfication if (inArray==null) throw new ArgumentNullException("inArray"); if (length<0) throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_Index")); if (offset<0) throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive")); inArrayLength = inArray.Length; if (offset > (inArrayLength - length)) throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength")); if( options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) { throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)options)); } if (inArrayLength == 0) return String.Empty; bool insertLineBreaks = (options == Base64FormattingOptions.InsertLineBreaks); //Create the new string. This is the maximally required length. stringLength = CalculateOutputLength(length, insertLineBreaks); string returnString = String.GetStringForStringBuilder(String.Empty, stringLength); fixed (char* outChars = returnString){ fixed (byte* inData = inArray) { int j = ConvertToBase64Array(outChars,inData,offset,length, insertLineBreaks); returnString.SetLength(j); return returnString; } } }
public unsafe static int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options) { if (inArray == null) { throw new ArgumentNullException("inArray"); } if (outArray == null) { throw new ArgumentNullException("outArray"); } if (length < 0) { throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (offsetIn < 0) { throw new ArgumentOutOfRangeException("offsetIn", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive")); } if (offsetOut < 0) { throw new ArgumentOutOfRangeException("offsetOut", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive")); } if (options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) { throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", new object[] { (int)options })); } int num = inArray.Length; if (offsetIn > num - length) { throw new ArgumentOutOfRangeException("offsetIn", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength")); } if (num == 0) { return 0; } bool insertLineBreaks = options == Base64FormattingOptions.InsertLineBreaks; int num2 = outArray.Length; int num3 = Convert.CalculateOutputLength(length, insertLineBreaks); if (offsetOut > num2 - num3) { throw new ArgumentOutOfRangeException("offsetOut", Environment.GetResourceString("ArgumentOutOfRange_OffsetOut")); } int result; fixed (char* ptr = &outArray[offsetOut]) { fixed (byte* ptr2 = inArray) { result = Convert.ConvertToBase64Array(ptr, ptr2, offsetIn, length, insertLineBreaks); } } return result; }
/// <summary> /// Create a Base64String from the byte[] /// </summary> /// <returns>string</returns> public string ToBase64String(Base64FormattingOptions formattingOptions) { return Convert.ToBase64String(_file, 0, _fileSize, formattingOptions); }
public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options) {}
/// <summary> /// Converts a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array /// encoded with base-64 digits. Parameters specify the subsets as offsets in the input and output arrays, the /// number of elements in the input array to convert, and whether line breaks are inserted in the output array. /// </summary> /// <param name="inArray">An input array of 8-bit unsigned integers.</param> /// <param name="offsetIn">A position within .</param> /// <param name="length">The number of elements of to convert.</param> /// <param name="outArray">An output array of Unicode characters.</param> /// <param name="offsetOut">A position within .</param> /// <param name="options">to insert a line break every 76 characters, or to not insert line breaks.</param> /// <returns>A 32-bit signed integer containing the number of bytes in .</returns> public static Int32 ToBase64CharArray(this Byte[] inArray, Int32 offsetIn, Int32 length, Char[] outArray, Int32 offsetOut, Base64FormattingOptions options) { return Convert.ToBase64CharArray(inArray, offsetIn, length, outArray, offsetOut, options); }
public static string ToBase64String (byte[] inArray, Base64FormattingOptions options) { if (inArray == null) throw new ArgumentNullException ("inArray"); return ToBase64String (inArray, 0, inArray.Length, options); }
public static string ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) { return default(string); }
public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options) { if (inArray == null) throw new ArgumentNullException ("inArray"); if (outArray == null) throw new ArgumentNullException ("outArray"); if (offsetIn < 0 || length < 0 || offsetOut < 0) throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0"); // avoid integer overflow if (offsetIn > inArray.Length - length) throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length"); if (length == 0) return 0; // note: normally ToBase64Transform doesn't support multiple block processing if (options == Base64FormattingOptions.InsertLineBreaks) { StringBuilder sb = ToBase64StringBuilderWithLine (inArray, offsetIn, length); sb.CopyTo (0, outArray, offsetOut, sb.Length); return sb.Length; } else { byte[] outArr = Base64Helper.TransformFinalBlock (inArray, offsetIn, length); char[] cOutArr = Encoding.ASCII.GetChars (outArr); // avoid integer overflow if (offsetOut > outArray.Length - cOutArr.Length) throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length"); Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length); return cOutArr.Length; } }
public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options) { Contract.Ensures(0 <= Contract.Result<int>()); return default(int); }
/// <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); }
private void WriteByteList(List<byte> bytes, Base64FormattingOptions options) { byte[] array = bytes.ToArray(); bytes = null; string base64 = Convert.ToBase64String(array, options); array = null; WriteObject(base64); }