namespace Cosmos.Common.Extensions { /// /// Contains various helper methods to convert ints to hex represented by string. /// Supported types: /// /// byte. /// ushort. /// int. /// uint. /// ulong. /// /// public static class ToHexString { //TODO: Can add several more overloads for other numbertypes, with and without width argument. /// /// Convert byte to 2 characters long hexadecimal string, padded with '0's. /// /// A byte to be converted to hexadecimal string. /// 2 characters long string value, padded with '0's. public static string ToHex(this byte n) { return ConvertToHex((uint)n, 2); } /// /// Convert byte to hexadecimal string of a given length. /// /// A byte to be converted to hexadecimal string. /// The number of characters in the resulting string. /// String value, right aligned and padded on the left with '0's. /// If aWidth is less then the length of the resulting string, the resulting /// string would not be trimmed. /// public static string ToHex(this byte n, int aWidth) { return ConvertToHex((uint)n, aWidth); } /// /// Convert int to 4 characters long hexadecimal string, padded with '0's. /// /// A int to be converted to hexadecimal string. /// 4 characters long string value, padded with '0's. public static string ToHex(this int n) { return ConvertToHex((uint)n, 4); //TODO: this cast might throw OverflowException. Better catch it. } /// /// Convert int to hexadecimal string of a given length. /// /// A int to be converted to hexadecimal string. /// The number of characters in the resulting string. /// String value, right aligned and padded on the left with '0's. /// If aWidth is less then the length of the resulting string, the resulting /// string would not be trimmed. /// public static string ToHex(this int n, int aWidth) { return ConvertToHex((uint)n, aWidth); } /// /// Convert 16-bit unsigned int to 4 characters long hexadecimal string, padded with '0's. /// /// A 16-bit unsigned int to be converted to hexadecimal string. /// 4 characters long string value, padded with '0's. public static string ToHex(this ushort n) { return ConvertToHex((uint)n, 4); } /// /// Convert 16-bit unsigned int to hexadecimal string of a given length. /// /// A 16-bit unsigned int to be converted to hexadecimal string. /// The number of characters in the resulting string. /// String value, right aligned and padded on the left with '0's. /// If aWidth is less then the length of the resulting string, the resulting /// string would not be trimmed. /// public static string ToHex(this ushort n, int aWidth) { return ConvertToHex((uint)n, aWidth); } /// /// Convert 32-bit unsigned int to 8 characters long hexadecimal string, padded with '0's. /// /// A 32-bit unsigned int to be converted to hexadecimal string. /// 8 characters long string value, padded with '0's. public static string ToHex(this uint aValue) { return ConvertToHex(aValue, 8); } /// /// Convert 32-bit unsigned int to hexadecimal string of a given length. /// /// A 32-bit unsigned int to be converted to hexadecimal string. /// The number of characters in the resulting string. /// String value, right aligned and padded on the left with '0's. /// If aWidth is less then the length of the resulting string, the resulting /// string would not be trimmed. /// public static string ToHex(this uint aValue, int aWidth) { return ConvertToHex(aValue, aWidth); } /// /// Convert 64-bit unsigned int to 16 characters long hexadecimal string, optionally padded with '0's. /// /// A 64-bit unsigned int to be converted to hexadecimal string. /// Determines if a left padding should be applied /// 16 characters long string value, optionally padded with '0's. public static string ToHex(this ulong aValue, bool withPadding = true) { var hex = ConvertToHex(aValue); if (withPadding) { return hex.PadLeft(16, '0'); } else { return hex; } } /// /// Convert 64-bit unsigned int to hexadecimal string of a given length. /// /// A 64-bit unsigned int to be converted to hexadecimal string. /// The number of characters in the resulting string. /// String value, right aligned and padded on the left with '0's. /// If aWidth is less then the length of the resulting string, the resulting /// string would not be trimmed. /// public static string ToHex(this ulong aValue, int aWidth) { return ConvertToHex(aValue).PadLeft(aWidth, '0'); } /// /// Get hex prefix. /// /// Hex prefix, as string. private static string GetPrefix() { return "0x"; } /// /// Get hex suffix. /// /// Hex suffix, as string. private static string GetSuffix() { return "h"; } /// /// Convert 32-bit unsigned int to hexadecimal string. /// /// A 32-bit unsigned int to be converted to hexadecimal string. /// String value. private static string ConvertToHex(uint num) { string xHex = string.Empty; if (num == 0) { xHex = "0"; } else { while (num != 0) { xHex = DigitToHexChar((byte)(num & 0xf)) + xHex; num = num >> 4; } } return xHex; } /// /// Convert 32-bit unsigned int to hexadecimal string of a given length. /// /// A 32-bit unsigned int to be converted to hexadecimal string. /// The number of characters in the resulting string. /// String value, right aligned and padded on the left with '0's. /// If aWidth is less then the length of the resulting string, the resulting /// string would not be trimmed. /// private static string ConvertToHex(uint aValue, int aWidth) { return ConvertToHex(aValue).PadLeft(aWidth, '0'); //TODO: PadLeft might throw ArgumentOutOfRangeException. Better catch it. } /// /// Convert 64-bit unsigned int to hexadecimal string. /// /// A 64-bit unsigned int to be converted to hexadecimal string. /// String value. private static string ConvertToHex(ulong num) { string xHex = string.Empty; while (num != 0) { //Note; char is converted to string because Cosmos crashes when adding char and string. Frode, 7.june. xHex = DigitToHexChar((byte)(num & 0xf)) + xHex; num = num >> 4; } return xHex; } /// /// Convert byte to hexadecimal char. /// /// A byte to be converted to hexadecimal char. /// Char value. public static char DigitToHexChar(byte d) { switch (d) { case 0: return '0'; case 1: return '1'; case 2: return '2'; case 3: return '3'; case 4: return '4'; case 5: return '5'; case 6: return '6'; case 7: return '7'; case 8: return '8'; case 9: return '9'; case 10: return 'A'; case 11: return 'B'; case 12: return 'C'; case 13: return 'D'; case 14: return 'E'; case 15: return 'F'; } return ' '; } } }