Skip to content

Commit 2caa5fe

Browse files
author
anlang
committed
增加字段标题和数值显示功能
1 parent 1b4ae88 commit 2caa5fe

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

src/ui/pages/bit_viewer.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,15 @@ impl BitViewerPage {
9999
let field_start_bit = bit_index;
100100
let actual_group_size = group_size.min(self.data.binary_bits().len() - bit_index);
101101

102-
// 显示字段标题
102+
// 显示字段标题和数值
103103
let field_title = if field_index < configured_fields_count {
104-
format!("字段 {} ({} 位):", field_index + 1, actual_group_size)
104+
let field_value = self.calculate_field_value(field_start_bit, actual_group_size);
105+
format!("字段 {} ({} 位): 0x{:X} {}",
106+
field_index + 1, actual_group_size, field_value, field_value)
105107
} else {
106-
format!("剩余位 ({} 位):", actual_group_size)
108+
let field_value = self.calculate_field_value(field_start_bit, actual_group_size);
109+
format!("剩余位 ({} 位): 0x{:X} {}",
110+
actual_group_size, field_value, field_value)
107111
};
108112

109113
ui.label(RichText::new(field_title).color(Color32::DARK_BLUE));
@@ -273,10 +277,25 @@ impl BitViewerPage {
273277
});
274278
}
275279
}
280+
281+
/// 计算指定字段的数值
282+
fn calculate_field_value(&self, start_bit: usize, bit_count: usize) -> u64 {
283+
let mut value = 0u64;
284+
for i in 0..bit_count {
285+
if start_bit + i < self.data.binary_bits().len() {
286+
if self.data.binary_bits()[start_bit + i] {
287+
value |= 1 << (bit_count - 1 - i);
288+
}
289+
}
290+
}
291+
value
292+
}
276293
}
277294

278295
impl Default for BitViewerPage {
279296
fn default() -> Self {
280297
Self::new()
281298
}
282299
}
300+
301+

0 commit comments

Comments
 (0)