Monday, April 29, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 5208  / 2 Years ago, fri, july 15, 2022, 7:51:08

I have a huge text file with digits representing an 8 bit value in binary. I want to convert these to hexadecimal (or decimal, doesn't really matter). That is, I have a text file containing e.g.



0 0 0 0  1 1 0 1
0 0 1 1 1 0 0 1
0 1 0 0 0 1 0 1
1 0 0 0 1 1 0 1


(just 8 bits) and I'd want to convert this to, say,



0x0d
0x39
0x45
0x1d


It sounds like this could be done with an awk script/vim macro, or a combination of any other tools.


More From » command-line

 Answers
1

Here a snippet of bash scripting to do that



while read line; do
n=$((2#${line// /}))
printf '0x%02X
' "$n"
done <input-file


where 2# means base 2 and ${var// /} means "remove all spaces"


[#43631] Saturday, July 16, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chilgirlguid

Total Points: 123
Total Questions: 114
Total Answers: 121

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;