to decimal - Two methods in MatlabBasics
convert a value from octal
to decimal (base 8 to base 10), we first need to
know what an octal
The octal system is a
system used in digital electronics.
In this system of numbers, the symbols are counted only from ‘0’ to
‘7’. In base 8 we only have eight different symbols (there are no
symbols such as '8' or '9').
Similarly, the decimal system
(base 10) has ten symbols, '0' to '9' and the binary
system (base 2) has only two symbols, '0' and '1'.
The following table shows
the meaning of all the symbols in
the octal system and
the equivalents in the decimal and binary systems.
Table equivalents: decimal, octal
and binary numbers
You can see that from 0
to 7 a number in octal is exactly the same as it is in decimal, but
what if you need to go beyond 7?
a video that explains you the concepts behind the numerical
equivalencies in different systems. After the video, we show you how to
code that in Matlab.
Solution 1. Conversion
from Octal to Decimal using the obvious way in Matlab
We can go from
decimal by using the function base2dec,
converts a base-N number string to a decimal
number. Its syntax is
The parameter base can be anything, not
For example, base2dec('100', 8) produces
a 64 in decimal and
It’s important to
remember that octal
numbers are treated as strings.
Solution 2. Conversion using a different
This is the idea that is
shown on the video.
Here we keep our octal number
o = '317'
The decimal equivalent starts being 0
Let's flip the octal symbol to align with the
exponents that follow
Now we create the appropriate exponent
for p = 0 :
length(o) - 1
is the necessary
multiplication and addition
achieve our idea, digit-by-digit
d = d
+ str2num(f(p+1)) * 8^p;
Now, we just display the result
If you run the code above, you'll get this result on screen (edited for clarity).
'Octal to decimal' to
'Octal to decimal' to 'Matlab Programming'