# Converting a list of binary numbers to a decimal numbers

5 views (last 30 days)

Show older comments

Dear Matlab Community,

for my study i have to convert a list of numbers from binary to decimal (fraction and integer).

I have tried this soultion: https://de.mathworks.com/matlabcentral/answers/467320-how-can-i-convert-a-binary-fraction-to-decimal

And thats my code:

close all;

clear all;

load('accumulator_output.dat');

accumulator_output_string = num2str(accumulator_output);

S = strsplit(accumulator_output_string,'.');

intV = S{1} - '0';

fracV = S{2} - '0';

intValue = intV * (2 .^ (numel(intV)-1:-1:0).')

fracValue = fracV * (2 .^ -(1:numel(fracV)).')

result = intValue + fracValue;

It works best for a single value in the list. How do I get Matlab to convert the whole list?

When I tried more than one value in the list, matlab will respons this errors:

Error using regexp

The 'STRING' input must be either a char row vector, a cell array of char row vectors, or a string array.

Error in strsplit (line 125)

[c, matches] = regexp(str, aDelim, 'split', 'match');

Error in Test (line 8)

S = strsplit(accumulator_output_string,'.');

Many thanks

-Martin

##### 0 Comments

### Accepted Answer

Jan
on 12 Aug 2022

Edited: Jan
on 12 Aug 2022

Replace

load('accumulator_output.dat');

accumulator_output_string = num2str(accumulator_output);

by

S = readlines('accumulator_output.dat');

To avoid the conversion of chars to numbers and back to chars and to numbers...

Avoid names like "accumulator_output_string", because they provoke typing errors.

Then process the string array element by element:

X = zeros(size(S));

for k = 1:numel(S)

X(k) = Binary2Dec(S{k});

end

function result = Binary2Dec(C)

S = strsplit(C, '.');

intV = S{1} - '0';

fracV = S{2} - '0';

intValue = intV * (2 .^ (numel(intV)-1:-1:0).');

fracValue = fracV * (2 .^ -(1:numel(fracV)).');

result = intValue + fracValue;

end

If you do have the code to convert 1 string, simply call it in a loop to do it for an array of strings.

##### 1 Comment

### More Answers (2)

John D'Errico
on 12 Aug 2022

Edited: John D'Errico
on 12 Aug 2022

You have a list of binary numbers, as a character array. So zeros, ones, and a decimal point. (Personally, I think that should be called a bicimal point.) For example:

BiNum = ['0001.1101';'0101.0111';'1000.0001';'0000.1000']

If we can assume all of the bicimal points lie at the same location, then it is truly trivial. First, locate the bicimal point.

bicimalind = strfind(BiNum(1,:),'.')

Once we know the location, we can remove them.

BiNum(:,bicimalind) = ''

Finally, reconstitute the numbers in true decimal form, first as integers.

BiNumBits = size(BiNum,2);

DecNum = (BiNum - '0')*2.^(BiNumBits-1:-1:0)'

All that remains now is to put the bicimal point back in, as an effective bit shift.

DecNum = DecNum*2^(bicimalind - BiNumBits - 1)

As you can see, fully vectorized. It only required that the bicimal points were in the same position for all numbers. If that was not true, then you would need to work slightly harder, but not a lot. You might do the above proces for every row, separately, in a loop. God forbid, a loop. In fact though, it would not take a lot of effort to do that in a vectorized form either.

A nice thing is, as long as your numbers are limited to no more than 52 bits, then these computations will result in EXACT values for those decimal numbers in double precision. That is because doubles are themselves stored in a binary form internally. In fact, this points out you could have done the above computations by converting the numbers into a proper hexadecimal form, then casting that result into a double. IMHO, probably more mental effort than it is worth, since the above computation was so easy.)

Finally, I would point out that IF your numbers have more than 52 bits, you will not be able to convert them in this way, since a double precision number is limited to 52 bits. Could it still be done, using carefully written code? Of course. I know that to be true, as the guy who has written several high precision arithmetic codes in MATLAB, so I know of what I speak. :)

##### 0 Comments

Jeffrey Clark
on 12 Aug 2022

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!