[SOLVED] Bash convert word with "echo" to binary and back?
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.
It's not 'only with `echo`', you're using `xxd` and `tr` as well. Please describe your problem more precisely. For example, what would be word 'AARDVARK' in binary?
As already asked, please describe in simple sentences, rather than with code, the problem you are actually trying to solve. Your descriptions so far seem ambiguous and inconsistent. For example, define exactly what you mean by "word" as used in your question.
Please review the Site FAQ for guidance in asking well formed questions. Especially visit the link from that page, How to Ask Questions the Smart Way for discussion of things to consider when asking others for help.
#!/bin/bash
for c; do printf "\\$(printf "%o" "$((2#$c))")"; done
does not work on multibyte chars (like €)
The only way I could think of is to use a eval and $'\u____'
bash functions for both directions:
Code:
str_to_binary()
{
# Converts a locale specific charset string, passed as arg1, into a string
# of unicode code-points using an ascii representation of base 2.
local arg="$1"
local i j n wchar
declare -A nibbles=(
[0]='0000' [1]='0001' [2]='0010' [3]='0011'
[4]='0100' [5]='0101' [6]='0110' [7]='0111'
[8]='1000' [9]='1001' [a]='1010' [b]='1011'
[c]='1100' [d]='1101' [e]='1110' [f]='1111'
)
for (( i=0 ; i < ${#arg} ; i++ ))
do
printf -v wchar '%x' "'${arg:i:1}"
for (( j=0 ; j < ${#wchar} ; j++ ))
do
n=${wchar:j:1}
printf '%s' ${nibbles[$n]}
done
printf " "
done
printf "\n"
}
binary_to_str()
{
# Converts a string of unicode code-points in an ascii representation,
# of base 2 back into a locale specific charset string.
local arg=$1
local byte ucode
for byte in $arg
do
if ucode=$(( 2#$byte )) ; then
printf -v ucode '\\u%x' "$ucode"
eval printf \'%s\' \$\'$ucode\'
else
>&2 printf "error: invalid binary value: %s\n" "$byte"
return 1
fi
done
return 0
}
Of course, this is an insane way to represent characters, so really, just for entertainments sake.
Last edited by GazL; 03-22-2022 at 06:29 AM.
Reason: missing \n on printf error line.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.