ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've always found it somewhat odd that the C Standard and the scanf("%i") format specifier don't support the use of 0b01000001. in the same way that you can use 0x41.
Yes if you're writing a program the various strtoXXX() functions can be used. It seems as if the OP here wants a command line to convert in the shell. But my contention is that they do not need to convert something which is already ASCII.
echo '01101000 01100101 01101100 01101100 01101111 00100000
01110111 01101111 01110010 01101100 01100100 00001010' |
while read -n9 byte; do # while read 9 characters
echo -ne \\x$( printf '%x' $((2#$byte)) ) # Convert binary to ascii
done
hello world
$((2#$byte)) converts from base 2 to base 10.
printf converts the base 10 to hex.
echo converts the hex to ascii characters.
This is the same as above except it converts non-printing characters to a dot and formats the output.
The non-printing characters are 00000000 to 00011111, and 01111111.
Code:
count=0
echo 0{0,1}{0,1}{0,1}{0,1}{0,1}{0,1}{0,1} | # Brace expansion creates 128 bytes 00000000 to 01111111
while read -n9 byte; do # while read 9 characters
[[ $byte =~ 000[01]{5}|01111111 ]] && echo -n '.' || # Replace non-printing with a dot or
echo -ne \\x$( printf '%x' $((2#$byte)) ) # convert binary to ascii
((++count%16==0)) && echo # Output 16 characters per line
done
................
................
!"#$%&'()*+,-./
0123456789:;<=>?
@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_
`abcdefghijklmno
pqrstuvwxyz{|}~.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.