to binary - Two methods in Matlab
convert a value from octal
to binary, we first need to know what an octal number is.
or base, used in digital electronics, is the octal system, also known
as base 8.
In this system of numbers, the symbols are counted only from ‘0’ to
‘7’. In base 8 we use only 8 different symbols.
(base 2) has only two symbols, '0' and '1'.
The following table shows
the meaning of all the symbols in
the octal system and
their conversion to
decimal or binary equivalents.
Table equivalents: decimal, octal
and binary numbers
To convert a value from
octal to binary, you merely
translate each octal symbol to the equivalent 3-bit binary group. For
the octal number 537 translates into the binary 101_011_111 equivalent.
Solution 1. Conversion
from Octal to Decimal, and then
Decimal to Binary
In Matlab, we can go from
decimal, and then, from decimal
to binary. We can use instructions ‘base2dec’ and
‘dec2bin’ for this purpose.
converts base N number string to decimal
number. Its syntax is d = base2dec('str', base). For example,
produces an 8.
Function dec2bin converts
a decimal number into a binary
string. Its syntax is b = dec2bin(d). For example, dec2bin(5) produces
We can embed one
instruction into the other, like this:
bin_str = dec2bin(base2dec(oct_str, 8))
So, we can use the
concept in this way:
= dec2bin(base2dec(oct_str, 8))
Matlab’s answer is:
bin_str = 101011111
It’s important to
remember that both binary numbers and octal
ones are treated as strings in Matlab.
Solution 2. Conversion to octal using a
Now, let’s say that we
want to explore how the octal
characters are separated to form the binary symbols and how we can
our own strings (binary and octal).
We can develop a function
to translate the table shown
before. Our proposed method uses a switch-case structure.
Octal to binary conversion
function b =
b = '000';
b = '001';
b = '010';
b = '011';
b = '100';
b = '101';
b = '110';
b = '111';
we have to call the function for every octal character
in the string, to convert each to a 3-bit binary number. One possible
to separate the octal strings into 3-bit groups is shown here:
octal number: ', 's');
for o = 1 :
= [bin_str o2b(oct_str(o))];
Let’s try this routine...
octal number: 1515
The result is:
octal number: 7070
The result is:
'Octal to binary' to
'Octal to binary' to 'Matlab Programming'