From 204e331ea23a289cf4ec04928c9f31e8d45f74c8 Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 3 Jul 2014 17:04:54 +0000 Subject: [PATCH] Oops, forgot to add this file --- BizHawk.Common/Extensions/NumberExtensions.cs | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 BizHawk.Common/Extensions/NumberExtensions.cs diff --git a/BizHawk.Common/Extensions/NumberExtensions.cs b/BizHawk.Common/Extensions/NumberExtensions.cs new file mode 100644 index 0000000000..b6349346da --- /dev/null +++ b/BizHawk.Common/Extensions/NumberExtensions.cs @@ -0,0 +1,66 @@ +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 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); + } + + /// + /// 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 int i) + { + if (i < 0x100) + { + return 2; + } + + if (i < 0x10000) + { + return 4; + } + + if (i < 0x1000000) + { + return 6; + } + + return 8; + } + } +}