Binary to decimal & Decimal to Binary Help
Show older comments
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
5 Comments
I don't get it. A number is always stored as a binary number (unless you have one of those contraptions that work in decimal notation). It's just that it's displayed in you screen as a base 10 number. If you want the accuracy of the decimal representation of a binary number, you can always use the builtin eps function.
Jan
on 9 Sep 2012
@Turgut: It does not get clear to me also. Please provide at least a full example of inputs and outputs.
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.
Answers (2)
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
4 Comments
Image Analyst
on 9 Sep 2012
dec2bin() ignores fractional parts:
>> dec2bin(4.1234)
ans =
100
Turgut
on 9 Sep 2012
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?
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.
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.
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!