gpt4 book ai didi

c# - WPF - 字节数组到十六进制 View (类似于 Notepad++ HEX-Editor 插件)

转载 作者:行者123 更新时间:2023-11-30 15:11:05 25 4
gpt4 key购买 nike

我正在使用 ItemsControl显示 List<byte>十六进制。 ItemsPanelTemplateUniformGrid具有固定数量的列:

<ItemsControl 
HorizontalAlignment="Left"
VerticalAlignment="Top"
ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="16"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StringFormat='\{0:X2\}'}" Margin="5,5,5,0"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

我想在每一行前加上一个“地址”列,就像您在 Notepad++“十六进制编辑器”插件中看到的那样。
也就是说,因为我有 16 列,所以每一行都应该有这样的前缀:

0000 [00 01 02 .... 0F]
0010 [10 11 12 .... 1F]
0020 [20 21 22 .... 2F]
...

有什么建议吗?

最佳答案

嗯..我终于继续并实现了一个 IValueConverter 将字节数组转换为格式良好的十六进制字符串:

/// <summary>
/// Converts a byte array to a multi-row hex string containing
/// byte offset indices as a prefix for each row.
/// </summary>
/// <example>
/// <![CDATA[
/// 0000: 13 31 DC 81 95 2C 92 E5
/// 0008: CE 14 F6 C7 2E CA 8F 13
/// 0010: 11 2E AB 80 2E 19 63 D1
/// 0018: 0D D6 88 2D 95 BF 03 FE
/// 0020: 6F 8F 2C 8A D8 A9 60 B4
/// ]]>
/// </example>
[ValueConversion(typeof(byte[]), typeof(String))]
public class ByteArrayToIndexedHexStringConverter : IValueConverter {

/// <summary>
/// Converts a byte array to a multi-row hex string.
/// Each row is prefixed with the starting byte offset and contains
/// a fixed number of bytes.
/// </summary>
/// <param name="value">The byte array to convert.</param>
/// <param name="targetType"></param>
/// <param name="parameter">A string, parsable to an integer, indicating
/// how many bytes per row. Typically 8 or 16.</param>
/// <param name="culture"></param>
/// <returns>A string that looks similar to the HexEdit plugin for Notepad++.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {

if (value == null) return null;

if (!(value is byte[])) {
throw new ArgumentException("'value' must be a byte array.");
}

int numBytesPerRow;
if ((parameter == null)
|| !(parameter is string)
|| !(int.TryParse((string)parameter, out numBytesPerRow))) {

throw new ArgumentException("'parameter' must be a string parsable to an integer (numBytesPerRow).");
}

var hexSplit = BitConverter.ToString((byte[])value)
.Replace('-', ' ')
.Trim()
.SplitIntoChunks(numBytesPerRow * 3)
.ToList();

int byteAddress = 0;
var sb = new StringBuilder();

for (int i = 0; i < hexSplit.Count; i++) {
sb.AppendLine(byteAddress.ToString("X4") + ":\t" + hexSplit[i]);
byteAddress += numBytesPerRow;
}

return sb.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotSupportedException();
}
}

我使用了 Jon 的 SplitIntoChunks实现。

关于c# - WPF - 字节数组到十六进制 View (类似于 Notepad++ HEX-Editor 插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2783378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com