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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Data Types » HEXScreenshots 
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>> 40x0f];
                outBytes[outOff + j + 1= hexTable[input[inOff0x0f];

                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 * 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.