Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Convert numeral systems (dec <-> bin <-> hex <-> oct)

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
Tags

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```
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```
# -------------------------------------------------------------------------- #
# -------------------------------------------------------------------------- #
• Binary
PHP Code:
``` # Convert hexadecimal to binary function hexToBin() {   in=\${1^^}   echo "ibase=16;obase=2;\$in" | BC_LINE_LENGTH=0 bc }  ```
Code:
```\$ hexToBin 13
10011```
• Decimal
PHP Code:
``` # Convert hexadecimal to decimal function hexToDec() {   in=\${1^^}   echo "ibase=16;\$in" | bc }  ```
Code:
```\$ hexToDec 13
19```
• Octal
PHP Code:
``` # Convert hexadecimal to octal function hexToOct() {   in=\${1^^}   echo "ibase=16;obase=8;\$in" | bc }  ```
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```
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```
PHP Code:
``` # Convert octal to hexadecimal function octToHex() {   echo "ibase=8;obase=20;\$1" | bc }  ```
Code:
```\$ octToHex 23
13```
Posted in General
« Prev     Main     Next »

All times are GMT -5. The time now is 10:37 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -