LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2022, 11:46 AM   #1
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Rep: Reputation: 0
Bash convert word with "echo" to binary and back?


Hello, is it possible to convert a word to binary and back to utf only with the command "echo" in bash, as in the following example?

Is it also possible to convert a word to hex and back to utf only with the "echo" command?

bash:
Code:
a=$(echo "test" | tr -d "\n" | xxd -p)
echo $a
echo $a | xxd -r -p
Thank you
 
Old 03-20-2022, 12:34 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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?

Last edited by NevemTeve; 03-20-2022 at 12:38 PM.
 
1 members found this post helpful.
Old 03-20-2022, 01:41 PM   #3
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
I have try this but have no input from word to integer.
Is there no way without xxd / bc / od like in the secound example?
Code:
z=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
echo ${z[65]}

st1="74657374"
for ((z=0; z<${#st1}; z+=2)); do a+=$(echo -ne $(echo ${st1:$z:2}) " "); done; st2=$a; unset a;
echo $st2
 
Old 03-20-2022, 01:52 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
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.
 
Old 03-20-2022, 02:21 PM   #5
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
conversion:
word="helloworld"

to:
results="01101000 01100101 01101100 01101100 01101111 01110111 01101111 01110010 01101100 01100100"

code example:
Code:
st1="helloworld"
EXECUTE="???" CODE TO EXECUTE
for ((z=0; z<${#st1}; z+=2))
    do 
        a+=$(echo -ne "$EXECUTE")
    done
st2=$a; unset a
echo $st2

Last edited by blumenwesen; 03-20-2022 at 02:26 PM.
 
Old 03-20-2022, 03:53 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
although it is not impossible it is not really supported by [pure] bash. Would be better to use an external converter (like od, xxd or similar)
 
Old 03-21-2022, 12:15 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
(Guess this is still the 'Shadowrun decker' project.)
 
Old 03-21-2022, 12:07 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
You can do this in Python pretty easily.
 
Old 03-21-2022, 06:14 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
The Python:
Code:
>>> "".join(f"{ord('h'):08b} " for c in "helloworld").strip()
'01101000 01101000 01101000 01101000 01101000 01101000 01101000 01101000 01101000 01101000'
 
Old 03-21-2022, 09:04 PM   #10
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,918

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
That was an interesting challenge. Pure bash solution:
Code:
#!/bin/bash

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 < ${#1} ; i++ ))
do
  byte=$( printf "%x" "'${1:i:1}" )
  for (( j=0 ; j < ${#byte} ; j++ ))
  do
	n=${byte:j:1}
	printf "%s" ${nibbles[$n]}
  done
  printf " "
done
printf "\n"
Code:
$ /tmp/binbash.sh "A€B"
01000001 0010000010101100 01000010 
$
'€' is unicode 20ac, so it gets more bits.
 
2 members found this post helpful.
Old 03-22-2022, 12:47 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by GazL View Post
That was an interesting challenge. Pure bash solution:
Yes, that was what I was thinking about (or at least something similar).

https://unix.stackexchange.com/quest...nversion-tools

And what about the other direction?
Code:
#!/bin/bash
for c; do printf "\\$(printf "%o" "$((2#$c))")"; done
does not work on multibyte chars (like €)
 
Old 03-22-2022, 06:07 AM   #12
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,918

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
Quote:
Originally Posted by pan64 View Post
And what about the other direction?
Code:
#!/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.
 
Old 03-22-2022, 08:58 AM   #13
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
thank you solved my problem.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regex for phrase like'word-word-word' Zero4 Linux - General 9 07-06-2019 06:36 AM
[SOLVED] Ping fails to a particular IP-address: "Echo Request" received but no "Echo Reply" sundialsvcs Linux - Networking 0 01-19-2017 06:44 PM
echo 0:$(echo 8*35*37*47 | bc) | xxd -r && echo $(id -un) Linuxanity LinuxQuestions.org Member Intro 1 08-15-2012 06:30 PM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration