Binary to decimal & Decimal to Binary Help

Asked by Turgut on 9 Sep 2012
Latest activity Commented on by José-Luis on 10 Sep 2012

Hello everyone;

I need to write a easy programme which take inputs of a decimal integer or decimal fractions (positive or negative) of a matrix and a bit value that want to be converted from decimal to binary than back decimal value.

I have written a programme for just one value:

```clear all
clc
a=input('Enter a decimal integer or a decimal with fractions: ');
n=input('Enter a bit value that want to be converted from decimal to binary than back decimal value: ');
for k=1:n;
a=a*2;
if (a<=1) y(k)=0
else
y(k)=1
a=a-1;
end
end
sum=0
for k=1:n;
sum=sum+y(k)*2^(-k)
end
```

This programme works for just a value but I couldn't bring the matrice values one by one then combine it.

For easy understand I want to explain with an example: Let f= [0.128 -1.35 0.489 3.547]

I need an fnew= [0.1275 -1.347 ... ] (not same just example)

I am trying to get error between the original and converted. I need your help please Thanks from now

Jan Simon on 9 Sep 2012

@Turgut: It does not get clear to me also. Please provide at least a full example of inputs and outputs.

Turgut on 9 Sep 2012

For example: Let A= [0.254 -1.569 4.624 0.147]

First we will convert these decimal numbers to binary. It gives approximately: Abin=[0.01000001000001100010010011011101 -1.10010001101010011111101111100111 100.10011111101111100111011011001000 0.00100101101000011100101011000000]

Now let's first take 4-bits of binary means:

Abin4=[0.0100 -1.1001 100.1001 0.0010]

Then convert back to decimal it gives: Adec4=[0.2500 -1.5625 4.5625 0.125]

As we see there are differences (errors) between the new converted and the original. I want to do these process. But not only 4-bits, for any bit number. When the bit number increases the error gets lower. I will show that.

Image Analyst on 9 Sep 2012

It almost looks like he's trying to round numbers to a specified number of decimal places, like .1275 rounded to 3 places is .128 however his output fnew has more places than the input f, so that can't be it otherwise you'd be making up additional decimal place numbers. I'm still not clear how he gets his fnew.

Products

Answer by José-Luis on 9 Sep 2012
Edited by José-Luis on 10 Sep 2012

ADD Here is a function that might return what you wanted. It involves manipulation of the ieee74 binary representation of a double. I did not consider the special case where your value is 0, and for some numbers the numerical precision might be an issue. numVals is the number of significant digits in the fractional part of the binary number that you want to keep

```function  [newVal sign biasExp frac] = your_fun(h,numVals)
```
```ieee74 = '';
```
```h = num2hex(h);
for ii =h
```
```switch ii
case {'0'}
b = '0000';
case {'1'}
b = '0001';
case {'2'}
b = '0010';
case {'3'}
b = '0011';
case {'4'}
b = '0100';
case {'5'}
b = '0101';
case {'6'}
b = '0110';
case {'7'}
b = '0111';
case {'8'}
b = '1000';
case {'9'}
b = '1001';
case {'A', 'a'}
b = '1010';
case {'B', 'b'}
b = '1011';
case {'C', 'c'}
b = '1100';
case {'D', 'd'}
b = '1101';
case {'E', 'e'}
b = '1110';
case {'F', 'f'}
b = '1111';
end
```
```ieee74 = [ieee74 b];
```
```end
```
```sign = ieee74(1);
biasExp = ieee74(2:12);
frac = ieee74(13:end);
```
```expVal = bin2dec(biasExp) - 1023;
```
```newVal = 1;
for ii = 1:numVals
newVal = newVal + str2num(frac(ii)) * 2^-ii;
end
```
```newVal = newVal * 2^expVal;
```
```if (sign == '1')
newVal = - newVal;
end
```

And for instance

```[newVal sign biasExp frac] = your_fun(0.128,4)
```
```newVal =
```
```0.125000000000000
```
```sign =
```
```0
```
```biasExp =
```
```01111111100
```
```frac =
```
```0000011000100100110111010010111100011010100111111100
```

Turgut on 9 Sep 2012

I agree with image analyst and it is my main problem that dec2bin doesn't proper with negative and fractional parts

Walter Roberson on 9 Sep 2012

There is no standard for how negatives or fractions are to be represented.

Should dec2bin use One's Complement, or should it use Separate Sign? If you have an N bit number that happens to have a leading 1, does that always indicate a negative binary number?

José-Luis on 10 Sep 2012

I edited my (erroneous) previous answer, but deleted everything by mistake. Oh well. Maybe this edited answer is closer to the mark. Also i have no idea whether the endianness of your system would affect the answer (my guess would be no), but num2hex's documentation does not say much about it.

Answer by Jan Simon on 10 Sep 2012
Edited by Jan Simon on 10 Sep 2012

Perhaps something like this, which rounds the value to the nearest power of 2:

```function R = RoundToPow2(X, N)
mult = 2 .^ N;
R    = round(X * mult) / mult;
```

1 Comment

Image Analyst on 10 Sep 2012

Perhaps:

```function test
clc;
f= [0.128 -1.35]
fnew= [0.1275 -1.347] % Desired output
R = RoundToPow2(f, 9)
```
```function R = RoundToPow2(X, N)
mult = 2 .^ N;
R = round(X * mult) / mult;
```

Results in command window:

```f =
0.1280   -1.3500
fnew =
0.1275   -1.3470
R =
0.1289   -1.3496
```

Doesn't give the fnew that he wants but he's never adequately explained what he wants.