LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > druuna
User Name
Password

Notices


- - - - - As of February 1st 2014 this blog is no longer updated - - - - -
Rate this Entry

Convert numeral systems (dec <-> bin <-> hex <-> oct)

Posted 12-10-2013 at 03:41 AM by druuna
Updated 01-31-2014 at 03:24 AM by druuna

Converting from one numeral system to another can be done using the bc command.

Here are a few examples in the form of functions:

# -------------------------------------------------------------------------- #
# - Decimal to ....
# -------------------------------------------------------------------------- #
  • Binary
PHP Code:
# Convert decimal to binary
function decToBin() {
  echo 
"obase=2;$1" bc

Code:
$ decToBin 19
10011
  • Hexadecimal
PHP Code:
# Convert decimal to hexadecimal
function decToHex() {
  echo 
"obase=16; $1" bc

Code:
$ decToHex 19
13
  • Octal
PHP Code:
# Convert decimal to octal
function decToOct() {
  echo 
"obase=8;$1" bc

Code:
$ decToOct 19
23
# -------------------------------------------------------------------------- #
# - Hexadecimal to ...
# -------------------------------------------------------------------------- #
  • Binary
PHP Code:
# Convert hexadecimal to binary
function hexToBin() {
  
in=${1^^}
  echo 
"ibase=16;obase=2;$inBC_LINE_LENGTH=0 bc

Code:
$ hexToBin 13
10011
  • Decimal
PHP Code:
# Convert hexadecimal to decimal
function hexToDec() {
  
in=${1^^}
  echo 
"ibase=16;$inbc

Code:
$ hexToDec 13
19
  • Octal
PHP Code:
# Convert hexadecimal to octal
function hexToOct() {
  
in=${1^^}
  echo 
"ibase=16;obase=8;$inbc

Code:
$ hexToOct 13
23
The above is based on bash4, if an older bash version is used change: in=${1^^}
to: in=$( echo $1 | tr '[:lower:]' '[:upper:]' )

# -------------------------------------------------------------------------- #
# - Binary to ...
# -------------------------------------------------------------------------- #
  • Decimal
PHP Code:
# Convert binary to decimal
function binToDec() {
  echo 
"ibase=2;$1" bc

Code:
$ binToDec 10011
19
  • Hexadecimal
PHP Code:
# Convert binary to hexadecimal
function binToHex() {
  echo 
"ibase=2;obase=10000;$1" bc

Code:
$ binToHex 10011
13
  • Octal
PHP Code:
# Convert binary to octal
function binToOct() {
  echo 
"ibase=2;obase=1000;$1" bc

Code:
$ binToOct 10011
23
# -------------------------------------------------------------------------- #
# - Octal to ...
# -------------------------------------------------------------------------- #
  • Binary
PHP Code:
# Convert octal to binary
function octToBin() {
  echo 
"ibase=8;obase=2;$1" bc

Code:
$ octToBin 23
10011
  • Decimal
PHP Code:
# Convert octal to decimal
function octToDec() {
  echo 
"ibase=8;obase=12;$1" bc

Code:
$ octToDec 23
19
  • Hexadecimal
PHP Code:
# Convert octal to hexadecimal
function octToHex() {
  echo 
"ibase=8;obase=20;$1" bc

Code:
$ octToHex 23
13
Posted in General
Views 3781 Comments 0
« Prev     Main     Next »

  



All times are GMT -5. The time now is 11:04 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration