public static string EncodeToHex(byte[] inputBytes, bool bUseSpaces) { ToHexTransform hexTransform = new ToHexTransform(); byte[] outputBytes = new byte[hexTransform.OutputBlockSize]; byte[] space = new byte[1] { (byte)' ' }; MemoryStream outputStream = new MemoryStream(); if (!hexTransform.CanTransformMultipleBlocks) { int inputOffset = 0; int inputBlockSize = hexTransform.InputBlockSize; while (inputBytes.Length - inputOffset > inputBlockSize) { hexTransform.TransformBlock( inputBytes, inputOffset, inputBytes.Length - inputOffset, outputBytes, 0); inputOffset += hexTransform.InputBlockSize; outputStream.Write( outputBytes, 0, hexTransform.OutputBlockSize); if (bUseSpaces) { outputStream.Write(space, 0, 1); } } outputBytes = hexTransform.TransformFinalBlock( inputBytes, inputOffset, inputBytes.Length - inputOffset); outputStream.Write(outputBytes, 0, outputBytes.Length); } string strRet = System.Text.Encoding.UTF8.GetString(outputStream.ToArray()); outputStream.Close(); return(strRet); }
public static string EncodeToHex(byte[] inputBytes, bool bUseSpaces) { ToHexTransform hexTransform = new ToHexTransform(); byte[] outputBytes = new byte[hexTransform.OutputBlockSize]; byte[] space = new byte[1] { (byte)' ' }; MemoryStream outputStream = new MemoryStream(); if (!hexTransform.CanTransformMultipleBlocks) { int inputOffset = 0; int inputBlockSize = hexTransform.InputBlockSize; while (inputBytes.Length - inputOffset > inputBlockSize) { hexTransform.TransformBlock( inputBytes, inputOffset, inputBytes.Length - inputOffset, outputBytes, 0); inputOffset += hexTransform.InputBlockSize; outputStream.Write( outputBytes, 0, hexTransform.OutputBlockSize); if (bUseSpaces) { outputStream.Write(space, 0, 1); } } outputBytes = hexTransform.TransformFinalBlock( inputBytes, inputOffset, inputBytes.Length - inputOffset); outputStream.Write(outputBytes, 0, outputBytes.Length); } string strRet = System.Text.Encoding.UTF8.GetString(outputStream.ToArray()); outputStream.Close(); return strRet; }