I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?
|
|
It's fairly straightforward to do the conversion from binary in pure bash ( Binary to decimal
Binary to hexadecimal
Going back to binary using bash alone is somewhat more complex, so I suggest you see the other answers for solutions to that. |
|||||||||||||||||||||
|
|
Assuming that by binary, you mean binary data as in data with any possible byte value including 0, and not base-2 numbers: To convert from binary,
Now, to convert back to binary, From the decimal output from
From the hexa
|
||||
|
|
|
You can use bc for this by manipulating the The trick is that you need to be explicit about the bases. So if your ibase is 2, then if you set your obase to 10, it won't do anything, as 10 in binary is 2. Hence you need to use hexadecimal notation. So binary to decimal would be (watch that obase is A) Binary to decimal:
Binary to hex:
|
|||||||||||||
|
|
for binary to hexadecimal use xxd tool in linux and for binary to decimal you can use qalculate tool. for help regarding xxd type xxd --help or man xxd in linux. |
|||
|
|
|
If you mean converting numbers from base-2 to 10 or 16 and back,
Some shells like
and so on. Both
But note that, when expanded,
to convert to binary. |
||||
|
|