V2/Cet/IO/Cet.IO/ByteArray/ByteArrayHelpers.Format.cs
/// <summary>
/// Parse a string containing an HEX representation to a byte-array
/// </summary>
/// <param name="source">The source string containing HEX characters.</param>
/// <returns>The resulting byte-array</returns>
/// <remarks>
///     <para>The conversion does not care about character casing.</para>
///     <para>Any character off the HEX-set is treated as separator.</para>
///     <para>The HEX-chars can be even without any separator (i.e. packed), 
///     however the conversion consider character pairs.</para>
/// </remarks>
/// <example>Sample behavior of the conversion:
///     <code>
///     byte[] b = ByteArrayHelpers.FromHex("0 zz@ q1 23 04 567 8! 9 abcdef");
/// 
///     for (int i = 0; i &lt; b.Length; i++)
///     {
///         Console.Write(b[i].ToString() + " ");
///     }
///     Console.WriteLine();
///
///     </code>
///     Yields the result:
///     <code>
///     0 1 35 4 86 7 8 9 171 205 239
///     </code>
/// </example>
/// <seealso cref="ToHex"/>
Parse a string containing an HEX representation to a byte-array
Parse a string containing an HEX representation to a byte - array
public static byte[] FromHex(string source)
        {
            if (_tblfromhex == null)
                InitFromHexConverter();
            var reader = new SimpleStringReader(source);
            var writer = new ByteArrayWriter();
            int nibble = 0;
            int digit = 0;
            int code;
            while ((code = reader.GetNext()) >= 0)
            {
                if (code > 0)
                {
                    digit = (digit << 8) | (code - 1);
                    nibble++;
                }
                else if (nibble > 0)
                {
                    nibble = 2;
                }
                if (nibble == 2)
                {
                    writer.WriteByte((byte)digit);
                    digit = 0;
                    nibble = 0;
                }
            }
            return writer.ToByteArray();
        }
public static byte [ ] FromHex ( string source ) { if ( _tblfromhex == null ) InitFromHexConverter ( ) ; var reader = new SimpleStringReader ( source ) ; var writer = new ByteArrayWriter ( ) ; int nibble = 0 ; int digit = 0 ; int code ; while ( ( code = reader . GetNext ( ) ) >= 0 ) { if ( code > 0 ) { digit = ( digit << 8 ) | ( code - 1 ) ; nibble ++ ; } else if ( nibble > 0 ) { nibble = 2 ; } if ( nibble == 2 ) { writer . WriteByte ( ( byte ) digit ) ; digit = 0 ; nibble = 0 ; } } return writer . ToByteArray ( ) ; }
Parse a string containing an HEX representation to a byte-array
Parse a string containing an HEX representation to a byte - array
//create a string reader to consume input chars //create a writer to accumulate the resulting bytes //consume characters until the end of string
The resulting byte-array
The resulting byte - array
source
string
The source string containing HEX characters.
The source string containing HEX characters .
remarks
The conversion does not care about character casing.Any character off the HEX-set is treated as separator.The HEX-chars can be even without any separator ,
however the conversion consider character pairs.
The conversion does not care about character casing . Any character off the HEX - set is treated as separator . The HEX - chars can be even without any separator however the conversion consider character pairs .
example
Sample behavior of the conversion.



for (int i = 0; i < b.Length; i++)
{
Console.Write(b[i].ToString() + " ");
}
Console.WriteLine().


Sample behavior of the conversion . for ( int i = 0 ; i < b . Length ; i ++ ) { Console . Write ( b [ i ] . ToString () + " " ) ; } Console . WriteLine () .
seealso
None