V2/Cet/IO/Cet.IO/ByteArray/ByteArrayHelpers.Format.cs
/// <summary>
/// Convert a byte-array to string containing its HEX-digit representation
/// </summary>
/// <param name="source">The byte-array to be converted.</param>
/// <param name="separator">
/// The optional characted to be used as separator. 
/// Use (char)0 for yielding a packed (no separator) resulting string.
/// </param>
/// <returns>The resulting string containing the HEX representation</returns>
/// <remarks>
///     <para>Each byte is represented as a character pair.</para>
///     <para>Whereas specified, pairs are separated by a custom character.</para>
/// </remarks>
/// <example>Sample behavior of the conversion:
///     <code>
///     byte[] b = { 12, 0, 34, 5, 67, 0, 89, 128, 192, 255 };
/// 
///     string s = ByteArraHelpers.ToHex(b);
///
///     Console.WriteLine(s);
///
///     </code>
///     Yields the result:
///     <code>
///     0C 00 22 05 43 00 59 80 C0 FF
///     </code>
/// </example>
/// <seealso cref="FromHex"/>
Convert a byte-array to string containing its HEX-digit representation
Convert a byte - array to string containing its HEX - digit representation
public static string ToHex(
            byte[] source,
            char separator = ' ')
        {
            int sourceCount = source.Length;
            if (sourceCount == 0)
            {
                return string.Empty;
            }
            else
            {
                int targetCount = (int)separator != 0
                    ? (sourceCount * 3 - 1)
                    : (sourceCount * 2);
                var target = new char[targetCount];
                byte value = source[0];
                target[0] = _tbltohex[unchecked(value >> 4)];
                target[1] = _tbltohex[unchecked(value & 0x0F)];
                int currentLength = 2;
                if ((int)separator != 0)
                {
                    for (int index = 1; index < sourceCount; index++)
                    {
                        value = source[index];
                        target[currentLength] = separator;
                        target[currentLength + 1] = _tbltohex[unchecked(value >> 4)];
                        target[currentLength + 2] = _tbltohex[unchecked(value & 0x0F)];
                        currentLength += 3;
                    }
                }
                else
                {
                    for (int index = 1; index < sourceCount; index++)
                    {
                        value = source[index];
                        target[currentLength] = _tbltohex[unchecked(value >> 4)];
                        target[currentLength + 1] = _tbltohex[unchecked(value & 0x0F)];
                        currentLength += 2;
                    }
                }
                return new String(target);
            }
        }
public static string ToHex ( byte [ ] source , char separator = ' ' ) { int sourceCount = source . Length ; if ( sourceCount == 0 ) { return string . Empty ; } else { int targetCount = ( int ) separator != 0 ? ( sourceCount * 3 - 1 ) : ( sourceCount * 2 ) ; var target = new char [ targetCount ] ; byte value = source [ 0 ] ; target [ 0 ] = _tbltohex [ unchecked ( value >> 4 ) ] ; target [ 1 ] = _tbltohex [ unchecked ( value & 0x0F ) ] ; int currentLength = 2 ; if ( ( int ) separator != 0 ) { for ( int index = 1 ; index < sourceCount ; index ++ ) { value = source [ index ] ; target [ currentLength ] = separator ; target [ currentLength + 1 ] = _tbltohex [ unchecked ( value >> 4 ) ] ; target [ currentLength + 2 ] = _tbltohex [ unchecked ( value & 0x0F ) ] ; currentLength += 3 ; } } else { for ( int index = 1 ; index < sourceCount ; index ++ ) { value = source [ index ] ; target [ currentLength ] = _tbltohex [ unchecked ( value >> 4 ) ] ; target [ currentLength + 1 ] = _tbltohex [ unchecked ( value & 0x0F ) ] ; currentLength += 2 ; } } return new String ( target ) ; } }
Convert a byte-array to string containing its HEX-digit representation
Convert a byte - array to string containing its HEX - digit representation
//get the number of bytes involved //no data to convert, however returns an empty string //create a char-array to fit exactly the resulting string //convert the first byte //convert the remaining buffer //insert the separator after each hex-digits pair //no separation between digits: the result will be packed
The resulting string containing the HEX representation
The resulting string containing the HEX representation
source
byte[]
The byte-array to be converted.
The byte - array to be converted .
separator
char
The optional characted to be used as separator.
Use (char)0 for yielding a packed (no separator) resulting string.
The optional characted to be used as separator . Use ( char ) 0 for yielding a packed ( no separator ) resulting string .
remarks
Each byte is represented as a character pair.Whereas specified, pairs are separated by a custom character.
Each byte is represented as a character pair . Whereas specified pairs are separated by a custom character .
example
Sample behavior of the conversion.



string s = ByteArraHelpers.ToHex(b).

Console.WriteLine(s).


Sample behavior of the conversion . string s = ByteArraHelpers . ToHex ( b ) . Console . WriteLine ( s ) .
seealso
None