using System.Linq; namespace BizHawk.Common.NumberExtensions { public static class NumberExtensions { public static string ToHexString(this int n, int numdigits) { return string.Format("{0:X" + numdigits + "}", n); } public static string ToHexString(this uint n, int numdigits) { return string.Format("{0:X" + numdigits + "}", n); } public static string ToHexString(this byte n, int numdigits) { return string.Format("{0:X" + numdigits + "}", n); } public static string ToHexString(this ushort n, int numdigits) { return string.Format("{0:X" + numdigits + "}", n); } public static string ToHexString(this long n, int numdigits) { return string.Format("{0:X" + numdigits + "}", n); } public static string ToHexString(this ulong n, int numdigits) { return string.Format("{0:X" + numdigits + "}", n); } public static bool Bit(this byte b, int index) { return (b & (1 << index)) != 0; } public static bool Bit(this int b, int index) { return (b & (1 << index)) != 0; } public static bool Bit(this ushort b, int index) { return (b & (1 << index)) != 0; } public static bool In(this int i, params int[] options) { return options.Any(j => i == j); } public static byte BinToBCD(this byte v) { return (byte)(((v / 10) * 16) + (v % 10)); } public static byte BCDtoBin(this byte v) { return (byte)(((v / 16) * 10) + (v % 16)); } /// /// Receives a number and returns the number of hexadecimal digits it is /// Note: currently only returns 2, 4, 6, or 8 /// public static int NumHexDigits(this long i) { //now this is a bit of a trick. if it was less than 0, it mustve been >= 0x80000000 and so takes all 8 digits if (i < 0) { return 8; } if (i < 0x100) { return 2; } if (i < 0x10000) { return 4; } if (i < 0x1000000) { return 6; } if (i < 0x100000000) { return 8; } return 16; } /// /// The % operator is a remainder operator. (e.g. -1 mod 4 returns -1, not 3.) /// public static int Mod(this int a, int b) { return a - (b * (int)System.Math.Floor((float)a / b)); } } }