Browse | Submit A New Snippet | Create A Package

 

BinaryDecimalConverter

Type:
Sample Code (HOWTO)
Category:
Other
License:
BSD License
Language:
Ruby
 
Description:
A small script to convert Binary to Decimal and the otherway arround

Versions Of This Snippet::

Lars Nielsen
Snippet ID Download Version Date Posted Author Delete
6411.02012-03-03 22:03Lars Nielsen
Changes since last version::
Simplified and more beautiful code
6310.12012-02-13 10:04Lars Nielsen

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :1.0

#!/usr/bin/env ruby
#Used to find the correct location of the ruby instalation
# BinaryDecimalConverter.rb
version = "BinaryDecimalConverter 1.0 - copyright h4xcode 2012 - BSD Licens" 
case ARGV[0]
when "-b"
  number = ARGV[1].to_i # makes the argument to an int
  print "Number in binary: " 
  print number.to_s(2)
  print "\n"
when "-d"
  number = ARGV[1].to_s
  print "Number in decimal: "
  print number.to_i(2)
  print "\n"
when "-v"
  puts version
else
  puts <<-EOF
Please provide command name

Usage:
  conbin -b decimal
  -b: Decimal should be a real number
  conbin -d binary 
  -d: binary should be a binary number
  conbin -v prints the version number and copyrigth information
EOF
end
		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..