Sunday, July 9, 2017

Working with various Base Numeric Systems



Nowadays people are more interested in programming languages like assembly language, shell scripting etc for pentesting or maybe automation processes. But unfortunately the people haven't mastered binary language consisting of 0's and 1's in which computer communicates.


In this post we will be talking about conversion of one base to another base numeric system which can be used in shell scripting for various purposes(pentesting, administration, etc.) to achieve their task.


We will be using a utility called 'bc' which is already available in linux for various conversion examples which consists of Decimal,Binary,Octal, Hexadecimal and various base numeric system.


- Decimal also called base 10 number system.

- Binary also called base 2 number system.
- Octal also called base 8 number system.
- Hex or Hexadecimal also called base 16 number system.

Throughout the post I will select only one number '23' which we will use to find out it's respective binary,Octal, Hexadecimal and various other base numeric representation.



Case 01 : Decimal to Binary



As you all know '23' is a decimal. So first let's convert decimal '23' to binary representation.
echo "obase=2;23" | bc

Figure 01 - Conversion from Decimal to Binary

Now let me explain the command to make it simplier for you inorder to understand without memorising.

echo prints the statement on the screen.

obase stands for output base. This is a special variable required by bc command which defines the output base value for a given number.
2 represents the base number system for binary
23 is the decimal number which I want to convert
| is the pipe (form of redirection used in linux)
bc is the utility which converts. (bc - An arbitrary precision calculator language)


Case 02 : Binary to Decimal


In this case we will convert the result obtained from previous case.

echo "ibase=2;10111"|bc

Figure 02 - Conversion from Binary to Decimal

The syntax is almost same for every case. The only difference is the keywords.
Here ibase stands for input base. This is a special variable required by bc command which defines the input base value provided for a given number.


Case 03 : Hexadecimal to Octal


In this case we will be converting the Hexadecimal representation for decimal '23' to octal.

echo "obase=8;ibase=16;17"|bc

Figure 03 - Conversion from Hexadecimal to Octal

Here I want my output in octal representation hence used obase with value '8'. The input value '17' is the hexadecimal (base 16) representation numeric system for the decimal number '23'.

I hope this post helps you out in making things easier to understand and work in a smarter way!


Extra's:


* Decimal - Hexadecimal : 

echo "obase=16;23"|bc


Figure 04 - Conversion from Decimal to Hexadecimal

* Hexadecimal - Decimal : 
echo "ibase=16;17"|bc


Figure 05 - Conversion from Hexadecimal to Decimal


* Binary - Octal
echo "obase=8;ibase=2;10111"|bc


Figure 06 - Conversion from Binary to Octal

* Octal - Binary : 
echo "obase=2;ibase=8;27"|bc

Figure 07 - Conversion from Octal to Binary

For your reference you can also use ascii manual page
man ascii

Figure 08 - Ascii Manual Page


No comments:

Post a Comment

Thanks for reading the post! Please leave your feedback here :)