Use at own risk. Programs haven't been thoroughly tested.

function: dec2bin

A simple BASH function convert a decimal to a binary string

Info

@author Kevin van Zonneveld
@version 0.7
@link http://kevin.vanzonneveld.net
@param (integer) INPUT the integer to convert

Example

Usage
dec2bin 256

Outputs
100000000

Source Code

download source
#!/bin/bash
#
 
function dec2bin { 
  input=$1
  result=""
  while [ $input -gt 0 ]; do
    if [ $((input%2)) -gt 0 ]; then
      result="1$result"
    else
      result="0$result"
    fi
    input=$((input/2))
  done;
}

Add comment

» Currently away on vacation. I can reply your message the 24th of July 2008. Please post anyway and check back then. Thank you!

for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

Comments

No comments. Be the first!