Facilities for outputting hexadecimal strings : 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Data Types » HEXScreenshots 
Facilities for outputting hexadecimal strings
 
/*  
 * PERWAPI - An API for Reading and Writing PE Files
 
 * Copyright (c) Diane Corney, Queensland University of Technology, 2004.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the PERWAPI Copyright as included with this
 * distribution in the file PERWAPIcopyright.rtf.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY as is explained in the copyright notice.
 *
 * The author may be contacted at d.corney@qut.edu.au
 
 * Version Date:  26/01/07
 */

using System;

namespace QUT.PERWAPI
{

    /// <summary>
    /// Facilities for outputting hexadecimal strings
    /// </summary>
    public class Hex
    {
        readonly static char[] hexDigit = {'0','1','2','3','4','5','6','7',
                                              '8','9','A','B','C','D','E','F'};
        readonly static uint[] iByteMask = 0x000000FF0x0000FF000x00FF00000xFF000000 };
        readonly static ulong[] lByteMask = {0x00000000000000FF0x000000000000FF00
                                                0x0000000000FF00000x00000000FF000000,
                                                0x000000FF000000000x0000FF0000000000,
                                                0x00FF0000000000000xFF00000000000000 };
        readonly static uint nibble0Mask = 0x0000000F;
        readonly static uint nibble1Mask = 0x000000F0;

        /// <summary>
        /// Derives a hexadecimal string for a byte value
        /// </summary>
        /// <param name="b">the byte value</param>
        /// <returns>hex string for the byte value</returns>
        public static String Byte(int b)
        {
            char[] str = new char[2];
            uint num = (uint)b;
            uint b1 = num & nibble0Mask;
            uint b2 = (num & nibble1Mask>> 4;
            str[0= hexDigit[b2];
            str[1= hexDigit[b1];
            return new String(str);
        }

        /// <summary>
        /// Derives a hexadecimal string for a short value
        /// </summary>
        /// <param name="b">the short value</param>
        /// <returns>hex string for the short value</returns>
        public static String Short(int b)
        {
            char[] str = new char[4];
            uint num1 = (uint)b & iByteMask[0];
            uint num2 = ((uint)b & iByteMask[1]) >> 8;
            uint b1 = num1 & nibble0Mask;
            uint b2 = (num1 & nibble1Mask>> 4;
            uint b3 = num2 & nibble0Mask;
            uint b4 = (num2 & nibble1Mask>> 4;
            str[0= hexDigit[b4];
            str[1= hexDigit[b3];
            str[2= hexDigit[b2];
            str[3= hexDigit[b1];
            return new String(str);
        }

        /// <summary>
        /// Derives a hexadecimal string for an int value
        /// </summary>
        /// <param name="val">the int value</param>
        /// <returns>hex string for the int value</returns>
        public static String Int(int val)
        {
            char[] str = new char[8];
            uint num = (uint)val;
            int strIx = 7;
            for (int i = 0; i < iByteMask.Length; i++)
            {
                uint b = num & iByteMask[i];
                b >>= (i * 8);
                uint b1 = b & nibble0Mask;
                uint b2 = (b & nibble1Mask>> 4;
                str[strIx--= hexDigit[b1];
                str[strIx--= hexDigit[b2];
            }
            return new String(str);
        }

        /// <summary>
        /// Derives a hexadecimal string for an unsigned int value
        /// </summary>
        /// <param name="num">the unsigned int value</param>
        /// <returns>hex string for the unsigned int value</returns>
        public static String Int(uint num)
        {
            char[] str = new char[8];
            int strIx = 7;
            for (int i = 0; i < iByteMask.Length; i++)
            {
                uint b = num & iByteMask[i];
                b >>= (i * 8);
                uint b1 = b & nibble0Mask;
                uint b2 = (b & nibble1Mask>> 4;
                str[strIx--= hexDigit[b1];
                str[strIx--= hexDigit[b2];
            }
            return new String(str);
        }

        /// <summary>
        /// Derives a hexadecimal string for a long value
        /// </summary>
        /// <param name="lnum">the long value</param>
        /// <returns>hex string for the long value</returns>
        public static String Long(long lnum)
        {
            ulong num = (ulong)lnum;
            return Long(num);
        }

        /// <summary>
        /// Derives a hexadecimal string for an unsigned long value
        /// </summary>
        /// <param name="num">the unsigned long value</param>
        /// <returns>hex string for the unsigned long value</returns>
        public static String Long(ulong num)
        {
            char[] str = new char[16];
            int strIx = 15;
            for (int i = 0; i < lByteMask.Length; i++)
            {
                ulong b = num & lByteMask[i];
                b >>= (i * 8);
                ulong b1 = b & nibble0Mask;
                ulong b2 = (b & nibble1Mask>> 4;
                str[strIx--= hexDigit[b1];
                str[strIx--= hexDigit[b2];
            }
            return new String(str);
        }

}

   
  
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.Hex Translator
5.Convert Hex char To Int
6.Hex To Unicode
7.Hex To Bytes
8.Unicode To Hex
9.Unicode To Hex (2)
10.Convert a hex string into a byte array with one byte for every 2 hex characters.
11.Convert Hex Value To Byte Array
12.Binary To Hex
13.Bytes To Hex String
14.Hex String To Bytes
15.Hex value to Binary
16.Is hex digit
17.Hex string to byte array and int
18.Convert Int To Plugwise Log Hex
19.Convert Plugwise Log Hex To Int
20.Convert a hexadecimal string to a byte array
21.Bytes To Hex String (2)
22.Hex String To Bytes (2)
23.Extends Encoding class to Represent hexadecimal (base-16) character encoding.
24.Bytes To Hex, Hex To StringBytes To Hex, Hex To String
25.Hex Encoding
26.Hex To Int
27.Calculates the HEX values of an array of bytes and produces a hex string
28.Bytes To Hex
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.