Hex Translator : HEX « Data Types « C# / C Sharp






Hex Translator

  
//http://www.bouncycastle.org/
//MIT X11 License

using System;

namespace Org.BouncyCastle.Utilities.Encoders
{
    /// <summary>
    /// A hex translator.
    /// </summary>
    public class HexTranslator 
    {
        private static readonly byte[]   hexTable =
            {
                (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
                (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
            };

        /// <summary>
        /// Return encoded block size.
        /// </summary>
        /// <returns>2</returns>
        public int GetEncodedBlockSize()
        {
            return 2;
        }

        /// <summary>
        /// Encode some data.
        /// </summary>
        /// <param name="input">Input data array.</param>
        /// <param name="inOff">Start position within input data array.</param>
        /// <param name="length">The amount of data to process.</param>
        /// <param name="outBytes">The output data array.</param>
        /// <param name="outOff">The offset within the output data array to start writing from.</param>
        /// <returns>Amount of data encoded.</returns>
        public int Encode(
            byte[]  input,
            int     inOff,
            int     length,
            byte[]  outBytes,
            int     outOff)
        {
            for (int i = 0, j = 0; i < length; i++, j += 2)
            {
                outBytes[outOff + j] = hexTable[(input[inOff] >> 4) & 0x0f];
                outBytes[outOff + j + 1] = hexTable[input[inOff] & 0x0f];

                inOff++;
            }

            return length * 2;
        }

        /// <summary>
        /// Returns the decoded block size.
        /// </summary>
        /// <returns>1</returns>
        public int GetDecodedBlockSize()
        {
            return 1;
        }

        /// <summary>
        /// Decode data from a byte array.
        /// </summary>
        /// <param name="input">The input data array.</param>
        /// <param name="inOff">Start position within input data array.</param>
        /// <param name="length">The amounty of data to process.</param>
        /// <param name="outBytes">The output data array.</param>
        /// <param name="outOff">The position within the output data array to start writing from.</param>
        /// <returns>The amount of data written.</returns>
        public int Decode(
            byte[]  input,
            int     inOff,
            int     length,
            byte[]  outBytes,
            int     outOff)
        {
            int halfLength = length / 2;
            byte left, right;
            for (int i = 0; i < halfLength; i++)
            {
                left  = input[inOff + i * 2];
                right = input[inOff + i * 2 + 1];

                if (left < (byte)'a')
                {
                    outBytes[outOff] = (byte)((left - '0') << 4);
                }
                else
                {
                    outBytes[outOff] = (byte)((left - 'a' + 10) << 4);
                }
                if (right < (byte)'a')
                {
                    outBytes[outOff] += (byte)(right - '0');
                }
                else
                {
                    outBytes[outOff] += (byte)(right - 'a' + 10);
                }

                outOff++;
            }

            return halfLength;
        }
    }

}

   
    
  








Related examples in the same category

1.The hex dump programThe hex dump program
2.To specify a hexadecimal constant, begin with 0x or 0X, followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F).
3.Hex Encoder
4.Convert Hex char To Int
5.Hex To Unicode
6.Hex To Bytes
7.Unicode To Hex
8.Unicode To Hex (2)
9.Convert a hex string into a byte array with one byte for every 2 hex characters.
10.Convert Hex Value To Byte Array
11.Binary To Hex
12.Bytes To Hex String
13.Hex String To Bytes
14.Hex value to Binary
15.Is hex digit
16.Hex string to byte array and int
17.Convert Int To Plugwise Log Hex
18.Convert Plugwise Log Hex To Int
19.Convert a hexadecimal string to a byte array
20.Bytes To Hex String (2)
21.Hex String To Bytes (2)
22.Extends Encoding class to Represent hexadecimal (base-16) character encoding.
23.Bytes To Hex, Hex To StringBytes To Hex, Hex To String
24.Hex Encoding
25.Hex To Int
26.Facilities for outputting hexadecimal strings
27.Calculates the HEX values of an array of bytes and produces a hex string
28.Bytes To Hex