Skip to content

Commit 1ef9474

Browse files
committed
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 <[email protected]>
1 parent 9cf9d76 commit 1ef9474

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

humility-probes-core/src/openocd.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ impl Core for OpenOCDCore {
7474

7575
fn read_word_32(&mut self, addr: u32) -> Result<u32> {
7676
let result = self.sendcmd(&format!("mrw 0x{:x}", addr))?;
77-
Ok(result.parse::<u32>()?)
77+
let rval = if let Some(hex_val) =
78+
result.strip_prefix("0x").or(result.strip_prefix("0X"))
79+
{
80+
u32::from_str_radix(hex_val, 16)?
81+
} else {
82+
result.parse::<u32>()?
83+
};
84+
Ok(rval)
7885
}
7986

8087
fn read_8(&mut self, addr: u32, data: &mut [u8]) -> Result<()> {
@@ -136,7 +143,14 @@ impl Core for OpenOCDCore {
136143
}
137144

138145
Some(idx) => {
139-
data[idx] = val.parse::<u8>()?;
146+
let dval = if let Some(hex_val) =
147+
val.strip_prefix("0x").or(val.strip_prefix("0X"))
148+
{
149+
u8::from_str_radix(hex_val, 16)?
150+
} else {
151+
val.parse::<u8>()?
152+
};
153+
data[idx] = dval;
140154
index = None;
141155
}
142156
}

0 commit comments

Comments
 (0)