user9013730
user9013730

Reputation:

Linux printf to convert Decimal to Binary?

It's very easy to convert Decimal number to Hex or Oct using printf function in Linux with %x and %o format specifier respectively.

user@linux:~$ file /usr/bin/printf
/usr/bin/printf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0
user@linux:~$ 

user@linux:~$ printf "Dec to Hex = %x\n" 255
Dec to Hex = ff
user@linux:~$ 

user@linux:~$ printf "Dec to Oct = %o\n" 255    
Dec to Oct = 377
user@linux:~$ 

I was trying to convert similar decimal number to binary but it didn't work

user@linux:~$ printf "Dec to Bin = %b\n" 255    
Dec to Bin = 255
user@linux:~$ 

Is it possible to convert Dec to Bin using printf function in Linux?

What else (number conversion) can be done with Linux printf?

Hex to Decimal? Hex to Octal? Hex to Binary?

Upvotes: 2

Views: 4249

Answers (3)

Sedat Kilinc
Sedat Kilinc

Reputation: 2961

Perl does a very good job in converting all number formats used in informatics to each other. I've written a shell script I use for myself and this is its most basic form:

convertnumber () {
    [ ${1} = 'd' -o ${1} = 'u' ] || local PREFIX="0${1}"
    perl -e "printf \"${PREFIX}%${1}\\n\", ${2}"
}

It shapes a string based on two parameters and which is then executed by the Perl interpreter.

The 1st parameter is the targeted number format,

e.g.

  • o -> octal
  • x -> hexadecimal
  • b -> binary
  • d -> decimal (u -> unsigned decimal)

The 2nd parameter is the number to be converted

which you have to write with its prefix, e.g.

  • 0o77

  • 0x3f

  • 0b111111

  • 63 (except decimal numbers)

    convertnumber 0 63
    
    convertnumber x 0o77
    
    convertnumber b 0x3f
    
    convertnumber d 0b111111
    

Negative numbers also work

Try the following if you want to see the

hexadecimal & octal representation of negative numbers

convertnumber x -1

convertnumber o -1

Upvotes: 0

Ped7g
Ped7g

Reputation: 16586

Is it possible to convert Dec to Bin using printf function in Linux?

No, C printf does not have format modifier for binary formatting ( http://www.cplusplus.com/reference/cstdio/printf/ ), and the linux util printf doesn't extend it with it either (the %b is "expand backslash escape sequences in the corresponding argument"). The printf tool may be still overshadowed by your particular shell, which may eventually add more formatters, then you have to check manual of your shell (for example bash does extend it somewhat, but still no "binary" formatter).

What else (number conversion) can be done with Linux printf?

Hex to Decimal? Hex to Octal? Hex to Binary?

It should support common C-formatted inputs (i.e. 0xN for hexadecimal input, and 0N for octal input), for example:

$ printf "%d %d %#x %#o\n" 0x10 010 16 8
16 8 0x10 010

So yes, you can basically convert decimal/hex/octal to decimal/hex/octal, any combination, by using desired input format and output format.

You are actually sort of thinking about it wrong, it's separate input to binary conversion, and binary to output, i.e. when you put "16" as argument value for printf, it's passed as string to the tool, it will then parse the string by value parser, detecting "16" is decimal format, converting it to native integer value 16 (i.e. binary inside CPU), and then it will finally convert it back to string according to the format specifier in the format string, i.e. "%#x" will produce hexadecimal formatting of that internal (binary) integer value ("0x10").

If you want "binary" output for yourself to check the value, seasoned assembly programmers don't bother with that, they learn to read hexadecimal per separate bits, because in hexadecimal every digit is formed by exactly 4 bits (0000 = 0x0, 0001 = 0x1, ... , 1110 = 0xE, 1111 = 0xF), so by using hexadecimal formatting you can quite easily read any particular bit you are interested into. While the true binary output makes it a bit easier by showing the 0/1 directly, but with 32 and 64 bit values it's a bit too long and makes it harder to stay position aware, this part is somewhat simpler in hexadecimal (as each two digits are 8 bits = one byte).

Upvotes: 1

ctac_
ctac_

Reputation: 2471

Cannot find a way with printf. You can use dc instead.

nb=255 ; dc -e '[dec to bin = ] 10 i 2 o '"$nb"' r n p'

dec to bin = 11111111

Upvotes: 1

Related Questions