CSharp examples for System:Byte Array
Convert byte array to PEM formatted string and set the heading as pem Header.
using Microsoft.Win32; using System.Text; using System.IO;/*from w w w. j a v a 2 s .co m*/ using System; public class Main{ /// <summary> /// Convert byte array to PEM formatted string and set the heading as pemHeader. /// </summary> /// <param name="data">source array.</param> /// <param name="pemHeader">PEM heading.</param> /// <returns>PEM formatted string.</returns> public static string BytesToPem(byte[] data, string pemHeader) { if (pemHeader == null || pemHeader.Length<1) { pemHeader = "ASN.1 Editor Generated PEM File"; } string retval = ""; if (pemHeader.Length > 0 && pemHeader[0] != ' ') { pemHeader = " " + pemHeader; } retval = Convert.ToBase64String(data); retval = FormatString(retval, 64, 0); retval = "-----BEGIN"+ pemHeader +"-----\r\n" + retval + "\r\n-----END"+ pemHeader +"-----\r\n"; return retval; } /// <summary> /// Convert byte array to PEM formatted string. /// </summary> /// <param name="data"></param> /// <returns></returns> public static string BytesToPem(byte[] data) { return BytesToPem(data, ""); } /// <summary> /// Format a string to have certain line length and character group length. /// Sample result FormatString(xstr,32,2): /// <code>07 AE 0B E7 84 5A D4 6C 6A BD DF 8F 89 88 9E F1</code> /// </summary> /// <param name="inStr">source string.</param> /// <param name="lineLen">line length.</param> /// <param name="groupLen">group length.</param> /// <returns></returns> public static string FormatString(string inStr, int lineLen, int groupLen) { char[] tmpCh = new char[inStr.Length*2]; int i, c = 0, linec = 0; int gc = 0; for (i=0; i<inStr.Length; i++) { tmpCh[c++] = inStr[i]; gc++; linec++; if (gc >= groupLen && groupLen > 0) { tmpCh[c++] = ' '; gc = 0; } if (linec >= lineLen) { tmpCh[c++] = '\r'; tmpCh[c++] = '\n'; linec = 0; } } string retval = new string(tmpCh); retval = retval.TrimEnd('\0'); retval = retval.TrimEnd('\n'); retval = retval.TrimEnd('\r'); return retval; } }