4

I have a HEX string in my bash script like the following:

1B6C00001AD615

I want to write this string in a binary file. (the binary representation of this string in a file that can be read with any normal hex editor for example)

I tried to use "bc" with ibase=16 and obase=2 , got the binary result in a string and tried to echo that to a file like in following example

echo -n -e 0100011000100101010101000010010

But when I try

vim -b myFile  

It does not appear as it should. Any suggestions>

1

1 Answer 1

2

You can always use bc for base conversions if you are interested in the binary representation of the value, e.g.

$ echo "obase=2; ibase=16; 1B6C00001AD615" | bc
11011011011000000000000000000000110101101011000010101

(note: your binary representation of the value in your question is incorrect)

Sign up to request clarification or add additional context in comments.

2 Comments

I had to do what you said + truncate the new lines and spaces that bc adds to split long values. Also I had to get that and echo it with "echo -e" to the file to make it work. I still have to verify if the output is exactly correct but I need to change some more stuff first. and YES :) my binary representation is incorrect for the reason why it was random and the representation of the HEX I wrote above. I just wanted to show what I want, not the actual result.
Sure, I understood what you wanted, no big deal on the binary in your question, I just wanted to make sure we were on the same page and if not, give you a chance to point it out. Glad it helped. Good luck with your scripting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.