# Binary to octal numbers: two solutions in Matlab

To convert a value from binary to octal, we first need to know what an octal number is.

 A major numbering system used in digital electronics is the octal system, also named base 8. In this system, the numbers are counted from 0 to 7. The following table shows the meaning of all the symbols in the bin and oct (for short) numbering systems.

Equivalent decimal, binary and octal numbers

To convert a value from binary to octal, you merely translate each 3-bit binary group to its octal equivalent. For example, the binary number 010 (or, to be clearly seen, 001 111 101 100 010 translates into the octal 17542 equivalent.

### Solution 1. Using built-in functions

In Matlab, we can go from binary to decimal, and then, from decimal to octal (using dec2base). We can embed one instruction into the other, like this:

oct_str = dec2base(bin2dec(bin_str), 8)

It’s important to remember that both binary numbers and octal ones are treated as strings in Matlab.

So, we can use this concept, like this:

bin_str = ''
oct_str = dec2base(bin2dec(bin_str), 8)

oct_str = 7542

### Solution 2. Separating in groups

Now, let’s say that we want to explore how the binary groups are separated to form the oct symbols and we want to manipulate our own strings (binary and octal).

We can develop a function to translate the table shown before. Our suggestion uses a switch-case structure.

% Binary to octal conversion
function
o = b2o(b)
switch b
case {'0', '00', '000'}
h =
'0';
case {'1', '01', '001'}
h =
'1';
case {'10', '010'}
h =
'2';
case {'11', '011'}
h =
'3';
case {'100'}
h =
'4';
case {'101'}
h =
'5';
case {'110'}
h =
'6';
case {'111'}
h =
'7';
end

Now, we have to call the function for every 3-bit group of binary numbers. One possible solution to separate the binary number into 3-bit groups is shown here:

bin_str = input('Enter binary number: ', 's');
i = length(bin_str);
n = ceil(i/3);

for g = n : -1 : 1
if i > 3
oct_str(g) = b2o(bin_str(i-3 : i));
i = i - 3;
else
oct_str(g) = b2o(bin_str(1 : i));
end
end

oct_str

Let’s try it.

Enter binary number: 101010
oct_str = 2A

Enter binary number: 1
oct_str = 62F5

Search Site

 Top Online Base Converter Hex to binary conversions Binary to Decimal Decimal to Binary Gray Code

## Related pages

curve fitting toolbox matlabhow to correct power factor with capacitorssine series expansionexponential regression linearea of irregular polygon calculatormortgage amortization tablematlab str2numboolean axiomsbinary to hexadecimal conversion methodc code for pascal triangleoctal to dechexadecimal to binary conversion tablescrap value in depreciationhexadecimal conversion to binaryformula to calculate factorialsquare root approximation formulawhat is the prime factorization for 49for loop example matlabthe heaviside functionnormal probability plot matlabbmi calculatingtime constant calculator rcmatlab least square fitmatlab defining functionsfor loop factorial3d graph in matlabcross product of two vectors in 2dfinding polynomial rootscode for pascal triangle in ckronecker delta functionhow to declare a function in matlabamortize mortgagebinary to grey codequad in matlabfind the sum of the given infinite geometric seriesascii table azmaclaurin series expansionsrc circuit time constantascii codes for alphabetspie chart matlabnumerical integral calculatorinterest calculator compounded continuouslyleast square fitting matlab3d mesh matlabascii symbol chartpolynomial least squares fitplotting a graph in matlabbmi examplesmatlab excelascii codes chartmatrix rotation matlabmatlab sample codesnumerical integral matlabbinary gray code conversionhow to sketch a piecewise functionpiecewise function exampleexamples of binary to decimalnormalized impedancediscount calculator formulaprogram lagrange interpolation matlabwhat is exponential regressionbiggest common divisorhow to calculate sq feet from inchesascii code for letter aevaluate integral calculatormatrices multiplication examplestransient analysis of rl and rc circuitscode for pascal trianglesubplot matlab examplematlab basic tutorialconvert binary number to decimal numbermatlab polynomial fitradioactive decay formula calculatorgrowth and decay calculatorsolve nonlinear equation matlabvolt ohm calculatorhow to do maclaurin series