Hiii...
I want to ask how to convert floating point to binary in MATLAB
Thank you

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 1월 7일

10 개 추천

e.g.:
a = 1234.57849; % your float point number
n = 16; % number bits for integer part of your number
m = 20; % number bits for fraction part of your number
% binary number
d2b = [ fix(rem(fix(a)*pow2(-(n-1):0),2)), fix(rem( rem(a,1)*pow2(1:m),2))]; %
% the inverse transformation
b2d = d2b*pow2([n-1:-1:0 -(1:m)].');
EDIT [16:32(UTC+4) 08.01.2012]
a = 1234.57849; % your float point number
n = 16; % number bits for integer part of your number
m = 25; % number bits for fraction part of your number
% binary number
d2b = fix(rem(a*pow2(-(n-1):m),2));
% the inverse transformation
b2d = d2b*pow2(n-1:-1:-m).';

댓글 수: 7

Walter Roberson
Walter Roberson 2012년 1월 7일
Been a while since I saw a 36 bit word ;-)
(Yes, I have used 36 bit computers.)
Julien Cohen
Julien Cohen 2016년 5월 20일
Much thanks (4 years later), this worked perfectly in my implementation of a genetic algorithm function.
OMID Fattemi
OMID Fattemi 2017년 2월 10일
for implementing GA, I used randint function to generate initial population as
d2b=randint(1,m+n);
then by the following code, decimal floating number is obtained:
b2d = d2b*pow2(n-1:-1:-m)';
Dave Amels
Dave Amels 2020년 3월 20일
DEC system 10
An Vo
An Vo 2020년 12월 3일
This code works perfectly for positive number. I run with negative number, the binary sequence contains bit 0, 1 and -1. bit -1 is not right. How can I fix this problem?
Walter Roberson
Walter Roberson 2020년 12월 3일
As discussed below, for negative values, you need to be specific about which representation you want to use for the binary fraction.
saba h
saba h 2021년 8월 8일
Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Oliver P
Oliver P 2016년 8월 10일

1 개 추천

Thank you for the cute and elegant solution! Unfortunately it's only working for positive floats. Negative floats will produce the same result as positive floats, but with negative bits. Which, of course, is not valid. And it's not the proper representation of negative values anyway.

댓글 수: 5

Walter Roberson
Walter Roberson 2016년 8월 10일
The proper representation of negative values? As decreed by which King?
Oliver P
Oliver P 2016년 8월 11일
편집: Oliver P 2016년 8월 11일
I'm not aware of a king here, but an association called IEEE. :-) The IEEE 754 format defines, for example, the leading bit convention. Matlab converts -22.9 properly to binary32 when you use:
dec2bin(typecast(single(-22.9), 'uint32'))
It's also possible to convert to binary64 in a similar way. But not to custom formats. Please do check this webpage for more info on this topic: IEEE 754 Converter
Walter Roberson
Walter Roberson 2016년 8월 11일
IEEE 754 defines one way to represent single precision numbers as binary, but it is far from being the only valid way.
When people ask about converting negative floating point to binary, the context is most typically the need to transmit quantized signals, which is almost always a fixed-point context, not a floating-point context. IEEE 754 does not deal with fixed point.
Oliver P
Oliver P 2016년 8월 12일
Yes, I agree. As far as I'm aware Matlab uses IEEE-754 for all floating-point (single, double and custom) and for unsigned fixed-point calculations. Only for signed fixed-point it's using two's-complement representation.
Walter Roberson
Walter Roberson 2016년 8월 12일
편집: Walter Roberson 2017년 2월 10일
The "Fixed Point Toolbox" can handle floating point numbers, but are only IEEE 754 if you request very specific formats.
I do not recall that the internal format for floating point number in the Symbolic Toolbox is documented.
The Fixed Point Toolbox offers Separated Sign. I would need to recheck to see if it offers One's Complement.

댓글을 달려면 로그인하십시오.

Muammar
Muammar 2012년 1월 8일

0 개 추천

Thank you for all answers...
Aneesh paulsagin
Aneesh paulsagin 2018년 3월 16일
편집: Walter Roberson 2020년 12월 3일

0 개 추천

convert complex number to binary number
A = [-0.0040383664156692-0.00294191598222591i, ...
1.00279327279556+0.00768012699728154i, ...
-0.00226521017869135+0.00526418383309796i, ...
0.999498954084202-0.007158248828685i, ...
-0.00549315262581557+0.00808461388120792i, ...
0.998352426774419+0.00927983415466687i, ...
0.00736345881927219+0.00540426830690426i, ...
0.989408434745709-0.0144762821959683i, ...
0.00827899268722473+0.0122398877118786i, ...
0.999298739008971-0.0129949269950415i, ...
-5.47057549608037e-07-0.0130605748664198i, ...
1.01414402334238+0.0131228156923076i, ...
0.000678728159952879-0.00434397278237206i, ...
0.985341332736134+0.0239798712601118i, ...
0.0109818351271128-0.00658607972360998i, ...
1.01709879921672-0.00394256645505557i, ...
0.000335417716939878-0.00461609765687651i, ...
0.996785178287252-3.51718069407279e-05i, ...
-0.0137042758344959+0.00734580139566216i, ...
1.01389851161064+0.00526816880638668i, ...
-0.0143246406043654-0.0173541476823603i, ...
0.984838248467196-0.00274924075252472i, ...
-0.00383017735389232-0.00877400220581385i, ...
0.996013541706753+0.0113592028562242i, ...
-0.00607963966107746-0.00701052911751136i, ...
1.00401827238935-0.0163653626342944i]

댓글 수: 4

A_binary = reshape(dec2bin(typecast(reshape([real(A(:).'); imag(A(:).')], 1, []),'uint64'),64).',1, []);
Ian Ono
Ian Ono 2021년 10월 18일
편집: Ian Ono 2021년 10월 18일
Walter Robinson, If I use your line to create A_binary and then fwrite it to file. How would I fread it back?
format long g
A = [
1.01709879921672-0.00394256645505557i, ...
0.996013541706753+0.0113592028562242i, ...
-0.00607963966107746-0.00701052911751136i, ...
]
A =
1.01709879921672 - 0.00394256645505557i 0.996013541706753 + 0.0113592028562242i -0.00607963966107746 - 0.00701052911751136i
A_binary_out = reshape(dec2bin(typecast(reshape([real(A(:).'); imag(A(:).')], 1, []),'uint8'),8).',1, []);
tn = tempname(); %temporary file name
fid = fopen(tn, 'w');
fwrite(fid, A_binary_out, 'char');
fclose(fid);
fid = fopen(tn, 'r');
A_binary_in = char(fread(fid, [1 inf], 'uint8'));
fclose(fid);
pairs = typecast(uint8(bin2dec(reshape(A_binary_in, 8, []).')),'double');
A_reconstructed = pairs(1:2:end) + 1i .* pairs(2:2:end)
A_reconstructed =
1.01709879921672 - 0.00394256645505557i 0.996013541706753 + 0.0113592028562242i -0.00607963966107746 - 0.00701052911751136i
If your original A did not happen to be a column vector, then you will need a step to reshape it to the original size.
Ian Ono
Ian Ono 2021년 10월 19일
Great!
Much thanks

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

질문:

2012년 1월 7일

댓글:

2021년 10월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by