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 = { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 };
readonly static ulong[] lByteMask = {0x00000000000000FF, 0x000000000000FF00,
0x0000000000FF0000, 0x00000000FF000000,
0x000000FF00000000, 0x0000FF0000000000,
0x00FF000000000000, 0xFF00000000000000 };
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