From 1ef9474989420c2f4f466761db23d6c36f469f48 Mon Sep 17 00:00:00 2001 From: Curt Brune Date: Thu, 14 Aug 2025 07:21:13 -0700 Subject: [PATCH] openocd: strip leading '0x' from hex numbers returned by openocd This patch strips leading '0x' from hex numbers (if present) returned by openocd for commands like `mem2array` and `mrw`. Fixes: #560 Signed-off-by: Curt Brune --- humility-probes-core/src/openocd.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/humility-probes-core/src/openocd.rs b/humility-probes-core/src/openocd.rs index d541398f..706f761b 100644 --- a/humility-probes-core/src/openocd.rs +++ b/humility-probes-core/src/openocd.rs @@ -74,7 +74,14 @@ impl Core for OpenOCDCore { fn read_word_32(&mut self, addr: u32) -> Result { let result = self.sendcmd(&format!("mrw 0x{:x}", addr))?; - Ok(result.parse::()?) + let rval = if let Some(hex_val) = + result.strip_prefix("0x").or(result.strip_prefix("0X")) + { + u32::from_str_radix(hex_val, 16)? + } else { + result.parse::()? + }; + Ok(rval) } fn read_8(&mut self, addr: u32, data: &mut [u8]) -> Result<()> { @@ -136,7 +143,14 @@ impl Core for OpenOCDCore { } Some(idx) => { - data[idx] = val.parse::()?; + let dval = if let Some(hex_val) = + val.strip_prefix("0x").or(val.strip_prefix("0X")) + { + u8::from_str_radix(hex_val, 16)? + } else { + val.parse::()? + }; + data[idx] = dval; index = None; } }