LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Convert a file of binery values to hex file (https://www.linuxquestions.org/questions/programming-9/convert-a-file-of-binery-values-to-hex-file-4175441931/)

voda87 12-18-2012 03:57 AM

Convert a file of binery values to hex file
 
Hi,

I have a file that looks like that:

11000010
00000000
00010001
00100000
11110111
00000000
10001100
00000001
10110110


and so on for thousands of lines...

i want to convert it to a file that looks like this:

FA D7 67 A6 0F 00 61 3C
F1 A3 47 3A 08 00 81 1C
DD 13 67 AA 5F 00 11 3C
FA D3 69 AA 0F 00 61 3B

i just made up the HEX values, they don't much in my example...

i found this to be what I'm looking for but for a single value :
Code:

echo "obase=16;ibase=2; 11000100" | bc
Thanks for your help guys.

MensaWater 12-18-2012 08:05 AM

Just do a while loop on your file:

Code:

cat file |while read binary
do echo "obase=16;ibase=2;$binary" |bc
done

That sets each line of the file to the variable named binary (you could name it anything) then uses that variable in the bc string command string you'd already figured out.


All times are GMT -5. The time now is 02:53 PM.