3

How to pad zero to binary numbers using shell script.

binary.txt

1010011100010001010111001101111
0
10110000000101000000000000001
10100000011
1000000100
1111111111111111
11111111111111110000000000000000
11111111111111110000000000000000
11111111111111110000000000000000
11111111111111110000000000000000
11111111111111110000000000000000
11111111111111110000000000000000
11111111111111110000000000000000
11111111111111110000000000000000
11111111111111110000000000000000
11111111111111110000000000000000

when i do the following

printf "%032s\n" $(<binary.txt)

I get a white space.

  1010011100010001010111001101111
                                0
    10110000000101000000000000001
                      10100000011
                       1000000100
                 1111111111111111
 11111111111111110000000000000000
 11111111111111110000000000000000
 11111111111111110000000000000000

But I need zero's appended. Any input is appreciated. thanks,

Updated:

binary.txt is my input file and has the following contents.

1010011100010001010111001101111
0
10110000000101000000000000001
10100000011
1000000100
1111111111111111
11111111111111110000000000000000
11111111111111110000000000000000

while running the following command in the command line it works fine.

   printf "%032s\n" $(<binary.txt) | tr ' ' '0' >> t1.mif

But when i try to do the same using a script below , it gives me wrong values. Any suggestions.

#!/bin/bash
FILE=binary.txt
while read line;do
    printf "%032s\n" $line | tr ' ' '0' >> t1.mif
done < $FILE

thanks

2
  • Which platform are you running on? I just tried printf "%032s\n" "110011" "001100" on Mac OS X and got what you'd wanted. Commented May 30, 2014 at 18:35
  • I am running it on linux platform. for some reason i don't see the zeros. Julienc answers works for me.. Commented May 30, 2014 at 18:37

3 Answers 3

5

You can use tr to replace the spaces you get with zeroes:

printf "%032s\n" $(<binary.txt) | tr ' ' '0'
Sign up to request clarification or add additional context in comments.

Comments

3

julienc's answer using tr is right, and the best portable solution, but I wanted to highlight that you hit an interesting difference between BSD and GNU implementations of printf.

For FreeBSD:

 `0' (zero)   Zero padding.  For all conversions except n, the converted value is
              padded on the left with zeros rather than blanks.

For GNU C in Linux:

 0            The value should be zero padded.  For d, i, o, u, x, X, a, A,
              e, E, f, F, g, and G conversions, the converted value is
              padded on the left with zeros rather than blanks.

Thus, your code will probably work in BSD implementations such as Mac OS X, but for portability you should use the above tr solution.

Note that switching to %d or %s is also out for the sake of overflow:

printf "%032d\n" "101010101010101010101010101010"
-bash: printf: warning: 101010101010101010101010101010: Result too large
00000000000009223372036854775807

Comments

2

Unfortunately printf does not support padding of strings - except with wide spaces. You can use the following awk script as an alternative.

awk '{for(i=length($0);i<32;i++){$0="0"$0}print}' bin.txt 

2 Comments

+1: Right OP could use %d or %.f but then the numbers would go out of range.
@jaypal yes, unfortunately they are getting "out of range".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.