How to convert byte[] to Hexadecimal format?

Hi everybody,
   Is there anyone who knows how to convert byte[] data into hexadecimal data and store it in a text file Or for that matters how to to convert int to hexadecimal In VB, there is method Hex() where you convert data to hexdecimal. I can't find exact method in C#. Advanced Thanks.

den2005



Answer this question

How to convert byte[] to Hexadecimal format?

  • jftl6y_007

    Hi,

    I just stumled on a code sample in MSDN:


    using System;

    class BlockCopyDemo
    {
        // Display the array contents in hexadecimal.
        public static void DisplayArray( Array arr, string name )
        {
            // Get the array element width; format the formatting string.
            int elemWidth = Buffer.ByteLength( arr ) / arr.Length;
            string format = String.Format( " {{0:X{0}}}", 2 * elemWidth );

            // Display the array elements from right to left.
            Console.Write( "{0,5}:", name );
            for( int loopX = arr.Length - 1; loopX >= 0; loopX-- )
                Console.Write( format, arr.GetValue( loopX ) );
            Console.WriteLine( );
        }

        public static void Main( )
        {
            // These are source and destination arrays for BlockCopy.
            short[ ] src  = { 258, 259, 260, 261, 262, 263, 264,
                              265, 266, 267, 268, 269, 270 };
            long[ ]  dest = { 17, 18, 19, 20 };

            Console.WriteLine( "This example of the \n" +
                "Buffer.BlockCopy( Array, int, Array, int, " +
                "int ) \nmethod generates the following output.\n" +
                "Note: The arrays are displayed from right to left.\n" );
            Console.WriteLine( "  Initial values of arrays:\r\n" );

            // Display the values of the arrays.
            DisplayArray( src, "src" );
            DisplayArray( dest, "dest" );

            // Copy two regions of source array to destination array,
            // and two overlapped copies from source to source.
            Console.WriteLine( "\n  Call these methods: \n\n" +
                " Buffer.BlockCopy( src, 5, dest, 7, 6 ),\n" +
                " Buffer.BlockCopy( src, 16, dest, 22, 5 ),\n" +
                "  (these overlap source and destination)\n" +
                " Buffer.BlockCopy( src, 4, src, 5, 7 ),\n" +
                " Buffer.BlockCopy( src, 16, src, 15, 7 ).\n" );
            Console.WriteLine( "  Final values of arrays:\n" );

            Buffer.BlockCopy( src, 5, dest, 7, 6 );
            Buffer.BlockCopy( src, 16, dest, 22, 5 );
            Buffer.BlockCopy( src, 4, src, 5, 7 );
            Buffer.BlockCopy( src, 16, src, 15, 7 );

            // Display the arrays again.
            DisplayArray( src, "src" );
            DisplayArray( dest, "dest" );
        }
    }

    /*
    This example of the
    Buffer.BlockCopy( Array, int, Array, int, int )
    method generates the following output.
    Note: The arrays are displayed from right to left.

      Initial values of arrays:

      src: 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102
     dest: 0000000000000014 0000000000000013 0000000000000012 0000000000000011

      Call these methods:

     Buffer.BlockCopy( src, 5, dest, 7, 6 ),
     Buffer.BlockCopy( src, 16, dest, 22, 5 ),
      (these overlap source and destination)
     Buffer.BlockCopy( src, 4, src, 5, 7 ),
     Buffer.BlockCopy( src, 16, src, 15, 7 ).

      Final values of arrays:

      src: 010E 010D 0D01 0C01 0B01 0A09 0108 0701 0601 0501 0404 0103 0102
     dest: 00000000000C010B 010A000000000013 0000000701060105 0100000000000011
    */

     
    Hope this helps.

    cheers,

    Paul June A. Domag



  • Tom PIC

    I don't think there's a SQL type for hex, I guess you'd store it as a varchar


  • mattdk

    Do the bytes contain chars that are hex, or do you want to turn thier integer values to hex string.Format will allow you to do the latter.


  • ElliottNess

    Thanks cgraus and Paul. Yes, it contains characters and Isolve it before I viewed these replies. Thanks again. Do anyone of you knows what is name of hexadecimal datatype in mySQl database or it is possible to store hexadecimal value

    den2005

  • Marc Kuperstein

    How about doing the reverse from hexadecimal data to byte[] data


    den2005


  • joe joseph

    Thanks cgraus. I think you are right. But I posted same question on mySQL Forum site.

    den2005

  • How to convert byte[] to Hexadecimal format?