public static void Main(string[] args)
string hexString = "00002710";
Console.WriteLine(hexString.SafeHexToInt());
public static class SensorTypeConverter
public static T SafeConvert<T>(this string stringValue, T defaultValue, bool isHex = false)
if (string.IsNullOrEmpty(stringValue)) return defaultValue;
return (T) Convert.ChangeType(stringValue, typeof(T));
public static int SafeHexToInt(this string hexValue)
if (String.IsNullOrEmpty(hexValue)) return 0;
return int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
public static float SafeHexToFloat(this string hexValue)
if (String.IsNullOrEmpty(hexValue)) return 0;
hexValue = hexValue.Replace("x", string.Empty);
float.TryParse(hexValue, System.Globalization.NumberStyles.HexNumber, null, out float result);
public static double SafeHexToDouble(this string hexValue)
if (String.IsNullOrEmpty(hexValue)) return 0;
hexValue = hexValue.Replace("x", string.Empty);
double.TryParse(hexValue, System.Globalization.NumberStyles.HexNumber, null, out double result);
public static decimal SafeHexToDecimal(this string hexValue)
if (String.IsNullOrEmpty(hexValue)) return 0;
hexValue = hexValue.Replace("x", string.Empty);
decimal.TryParse(hexValue, System.Globalization.NumberStyles.HexNumber, null, out decimal result);