61 lines
1.7 KiB
C#
61 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.UI.Xaml.Data;
|
|
using Microsoft.UI.Xaml.Markup;
|
|
|
|
namespace IPClab3.Converters;
|
|
|
|
public class MemoryAddressConverter : IValueConverter
|
|
{
|
|
|
|
public object Convert(object value, Type targetType, object parameter, string language)
|
|
{
|
|
if (value is IntPtr ptr)
|
|
{
|
|
if (ptr == IntPtr.Zero)
|
|
return "0x0";
|
|
|
|
ulong numericAddress = (ulong)ptr;
|
|
|
|
// Format dynamically: 8 digits for 32-bit (x86), 16 digits for 64-bit (x64)
|
|
string hexFormat = IntPtr.Size == 4 ? "X8" : "X16";
|
|
return $"0x{numericAddress.ToString(hexFormat, CultureInfo.InvariantCulture)}";
|
|
}
|
|
|
|
return "0x0";
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, string language)
|
|
{
|
|
if (value is string addressStr)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(addressStr))
|
|
return IntPtr.Zero;
|
|
|
|
string cleanInput = addressStr.Trim();
|
|
if (cleanInput.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
cleanInput = cleanInput.Substring(2);
|
|
}
|
|
|
|
if (!ulong.TryParse(cleanInput, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong numericAddress))
|
|
{
|
|
return IntPtr.Zero;
|
|
}
|
|
|
|
// Architecture safety overflow check
|
|
if (IntPtr.Size == 4 && numericAddress > uint.MaxValue)
|
|
{
|
|
return IntPtr.Zero;
|
|
}
|
|
|
|
return (IntPtr)numericAddress;
|
|
}
|
|
|
|
return IntPtr.Zero;
|
|
}
|
|
} |