This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HexConverter.cs
57 lines (50 loc) · 1.65 KB
/
HexConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Data;
namespace FETHArchiveManager
{
[ValueConversion(typeof(long), typeof(long))]
public class HexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return Binding.DoNothing;
long size;
if (!long.TryParse(value.ToString(), out size))
return Binding.DoNothing;
StringBuilder sb = new StringBuilder(size.ToString("X8"), 10);
sb.Insert(0, "0x");
return sb.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = value as string;
if (String.IsNullOrEmpty(strValue))
return Binding.DoNothing;
Regex hexRegex = new Regex("^0x[0-9A-F]{1,8}$");
Match match = hexRegex.Match(strValue);
if (match.Success)
return System.Convert.ToInt64(strValue, 16);
else
{
if (IsAllDigits(strValue))
return strValue;
else
return Binding.DoNothing;
}
//throw new NotImplementedException();
}
bool IsAllDigits(string s)
{
foreach (char c in s)
{
if (!char.IsDigit(c))
return false;
}
return true;
}
}
}