Hey everyone, I'm brand new to this forum! 😊 I'm working as a junior embedded software engineer and facing some challenges with data collection at runtime. I use a debugger that sends hex values of a variable to my PC through USB, but since these values are interpreted as ASCII, I'm seeing a bunch of unreadable characters on the terminal.
Here's my question: Is there a way for me to set up a script that acts as an intermediary between the debugger and the terminal? I want to convert these hex values to their readable ASCII equivalents before they display on the terminal. For example, if I send 0x0123, I'd like the terminal to show "291" instead of the weird symbols. Also, any recommendations on materials to study for a solid grasp of bash scripting would be appreciated!
Thanks a ton for your help – I hope I'm making sense!
2 Answers
You can definitely use a Bash script to convert those hex values into something more readable! Here's a sample script:
```bash
#!/usr/bin/env bash
set -o errexit
LANG=C
while IFS= read -r -n 1 -d ''; do
if [[ $REPLY ]]; then
printf -v dec '%d' "'$REPLY"
else
dec=0
fi
printf 'dec: %-3s ' "$dec"
printf 'hex: %02x ' "$dec"
printf 'n'
done
```
You can run it like this:
# Copy the script into a file named `my_converter.bash`
# Make it executable: `chmod +x my_converter.bash`
# Then use: `debugger 2>&1 | my_converter.bash`
This way, it reads your debugger's output and converts it into decimal and hex for display!
Have you tried using the *od* command? It's a standard Linux tool that can handle displaying your input in different formats. You can pipe your data into it easily. Just check out the man page with *man od* for more options! If you share how you're currently acquiring your data, I might be able to suggest more methods to visualize it.
Thanks for the tip! I actually used the `od -h` command on a log file filled with unreadable symbols, and it started to show me some understandable output. Just to clarify, I'm working with a J-Link connected to a Cortex-M23 MCU, using the J-Link RTT for data output.
Thanks for the script! Quick question: what does the "debugger 2>&1" part do in the command?