Convert::ToBase64CharArray Method (array<Byte>, Int32, Int32, array<Char>, Int32)
Updated: June 2011
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, and the number of elements in the input array to convert.
Assembly: mscorlib (in mscorlib.dll)
public: static int ToBase64CharArray( array<unsigned char>^ inArray, int offsetIn, int length, array<wchar_t>^ outArray, int offsetOut )
Parameters
- inArray
- Type: array<System::Byte>
An input array of 8-bit unsigned integers.
- offsetIn
- Type: System::Int32
A position within inArray.
- length
- Type: System::Int32
The number of elements of inArray to convert.
- outArray
- Type: array<System::Char>
An output array of Unicode characters.
- offsetOut
- Type: System::Int32
A position within outArray.
| Exception | Condition |
|---|---|
| ArgumentNullException | inArray or outArray is nullptr. |
| ArgumentOutOfRangeException | offsetIn, offsetOut, or length is negative. -or- offsetIn plus length is greater than the length of inArray. -or- offsetOut plus the number of elements to return is greater than the length of outArray. |
The subset of length elements of inArray starting at position offsetIn, are taken as a numeric value and converted to a subset of elements in outArray starting at position offsetOut. The return value indicates the number of converted elements in outArray. The subset of outArray consists of base-64 digits.
The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", the lowercase characters "a" to "z", the numerals "0" to "9", and the symbols "+" and "/". The valueless character, "=", is used for trailing padding.
The offset and length parameters are 32-bit signed numbers. The offsetIn and offsetOut parameters are zero-based array positions.
Important Note: |
|---|
The ToBase64CharArray method is designed to process a single byte array that contains all the data to be encoded. To create a base-64 character array from a byte stream, use the System.Security.Cryptography::ToBase64Transform class. |
The following example demonstrates using the ToBase64CharArray method to UUencode (encode in base 64) a binary stream, then save the encoding to a file.
public: void EncodeWithCharArray() { FileStream^ inFile; array<Byte>^binaryData; try { inFile = gcnew FileStream( inputFileName, FileMode::Open, FileAccess::Read ); binaryData = gcnew array<Byte>((int)(inFile->Length)); long bytesRead = inFile->Read( binaryData, 0, (int)inFile->Length ); inFile->Close(); } catch ( Exception^ exp ) { // Error creating stream or reading from it. Console::WriteLine( "{0}", exp->Message ); return; } // Convert the binary input into Base64 UUEncoded output. // Each 3 Byte sequence in the source data becomes a 4 Byte // sequence in the character array. long arrayLength = (long)((4.0 / 3.0) * binaryData->Length); // If array length is not divisible by 4, go up to the next // multiple of 4. if ( arrayLength % 4 != 0 ) { arrayLength += 4 - arrayLength % 4; } array<Char>^ base64CharArray = gcnew array<Char>(arrayLength); try { Convert::ToBase64CharArray( binaryData, 0, binaryData->Length, base64CharArray, 0 ); } catch ( ArgumentNullException^ ) { Console::WriteLine( "Binary data array is null." ); return; } catch ( ArgumentOutOfRangeException^ ) { Console::WriteLine( "Char Array is not large enough." ); return; } // Write the UUEncoded version to the output file. StreamWriter^ outFile; try { outFile = gcnew StreamWriter( outputFileName, false, Text::Encoding::ASCII ); outFile->Write( base64CharArray ); outFile->Close(); } catch ( Exception^ exp ) { // Error creating stream or writing to it. Console::WriteLine( " {0}", exp->Message ); } }
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Important Note: