inverting a binary


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting inverting a binary
# 1  
Old 09-08-2011
inverting a binary

Hi ,

I have a binary in a variable and i want to invert it and store in a new variable. i mean, replace 0 with 1 and vice versa. I tried reading character by character with the below script but it didnt provide me the proper result.

Code:
#!/bin/bash
count=1
var1="00100011"
while [[ $count -le ${#var1} ]]
do
     var2=$(expr substr $var1 $count 1)
     if [ "$var2" -eq 1 ];
        then
        var3=0
     else
        var3=1
        echo $var3
        count=`expr $count + 1`
     fi
     done

please suggest a solution for this.

Last edited by radoulov; 09-08-2011 at 09:24 AM.. Reason: Code tags!
# 2  
Old 09-08-2011
Code:
echo "$var1" | sed 's 0 - g; s 1 0 g; s - 1 g'

Homework?

Last edited by yazu; 09-08-2011 at 09:40 AM..
# 3  
Old 09-08-2011
Hi.

Modifying the OP code:
Code:
#!/bin/bash
count=1
var1="00100011"
printf " Original: $var1\n"
printf " Inverted: "
while [[ $count -le ${#var1} ]]
do
  var2=$(expr substr $var1 $count 1)
  if [ "$var2" -eq 1 ];
  then
    var3=0
  else
    var3=1
  fi
  # echo $var3
  printf $var3
  count=`expr $count + 1`
done
printf "\n"

producing:
Code:
 Original: 00100011
 Inverted: 11011100

Think about how the changes made it work correctly ... cheers, drl
# 4  
Old 09-08-2011
This task is tailor made for tr:
Code:
tr 01 10

Regards,
Alister
# 5  
Old 09-08-2011
Just to add using sed
Code:
echo 101101 | sed 'y/01/10/'
010010

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Binary Comparision

Hi Folks, Is there any way to compare the binaries which are built in AIX (5.3.0) environment? Thanks in advance. MKR (4 Replies)
Discussion started by: MKR
4 Replies

2. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

3. Shell Programming and Scripting

Inverting positions of variable & pattern

Hi, Can somebody help me to know how to invert the positions of this construct? ${variable%pattern} to ${pattern%variable} for pattern matching positions. Example 1 var=testcase echo {var%e} testcas So I am not sure how to invert variable and pattern so I can do something like this... (4 Replies)
Discussion started by: bartsimpsong
4 Replies

4. Shell Programming and Scripting

binary to ascii

Hi, Is there a way to convert the binary file to ascii . the binary file is pipe delimited. from source the file(pipe delimited) is ftped to mainframe and from mainframe it is ftped to the unix box using binary format. Is there a way to change it back to ascii and view it? Thanks! (3 Replies)
Discussion started by: dnat
3 Replies

5. Shell Programming and Scripting

Parsing Binary

I have a binary file a particular format. It contains the Length Bytes and the Type bytes i.e the first four bytes if the file indicate the length of the Type which is to follow. for eg, if the int value of the first four bytes is 80, then it means that the length of the following "Type" is 80.... (2 Replies)
Discussion started by: xgringo
2 Replies

6. Solaris

compiled binary file gives "cannot execute binary file"

Hi, I have two Solaris machines. 1. SunOS X 5.8 Generic_108528-29 sun4u sparc SUNW,Sun-Blade-1500 2. SunOS Y 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-60 I am trying to buiild a project on both these machines. The Binary output file compiled on machine 2 runs on both the machines. Where... (0 Replies)
Discussion started by: scgupta
0 Replies

7. UNIX for Dummies Questions & Answers

binary file

when using telnet localhost 25 I can cat ASCII files into the email body and an attachment, but how do i get a non-ASCII file into it, like a picture or a word document (.doc not rtf). uuencode, just stalls. Any ideas? :confused: (1 Reply)
Discussion started by: markms
1 Replies

8. UNIX for Dummies Questions & Answers

binary file

please let me know how can i mail the binary files is it can be done thru pine? is there any other way to do it? wat are the changes in system i have to make and one more thing i am sending data to a message queue and then retriving the data from the queue but when i do the ipcs... (1 Reply)
Discussion started by: ramneek
1 Replies

9. UNIX for Dummies Questions & Answers

Where is M4 binary?

Hello, I am configurating Sendmail on Mac OS 10.x terminal. I tried to execute m4 to generate a new sendmail.cf. It complains "Command not found". Anybody knows where the m4 binary is? Is it something coming along with Unix or Sendmail? Appreciate any help. Thanks in advance. pw (2 Replies)
Discussion started by: hypamw
2 Replies
Login or Register to Ask a Question