?
Solved

MATLAB- decimal to binary convert.....?

Posted on 2003-03-16
4
Medium Priority
?
25,812 Views
Last Modified: 2007-12-19
Hi,

I need to write a program that converts a positive decimal number to a binary string.
I have managed to do this for numbers smaller than 64, but after that matlab turns to scientific notation as I am effectively adding 1, 10, 100, 1000, 10000, etc. at 64 it should read 1000000 instead it gives 1e+006.

The code I have for this is:



clear

clc

num=input('Enter a  positive number: ');
num2=num;

while (num<0)
    clc
    fprintf('Invalid entry!\n');
    a=input('Enter a positive number: ');
end

a=1;
c=num;
d=0;

while (c>=1)
    b=rem(c,2);
    c=c/2;
    c=floor(c);
    d=d+(b*a);
    a=a*10;
end

lsb=rem(num,2);
msb=b;
   
fprintf('\n\n%g is written %g in binary form.\n\n',num2,d)
fprintf('The least-significant bit is %g\n\n',lsb)
fprintf('The most-significant bit is %g\n\n',msb)



____________________________________________

I have also managed to get it to display the binary number in reverse:




clear

clc

num=input('Enter a  positive number: ');
num2=num;

while (num<0)
    clc
    fprintf('Invalid entry!\n');
    a=input('Enter a positive number: ');
end

fprintf('\n\n%g is written ',num2)

c=num;
d=0;

while (c>=1)
    b=rem(c,2);
    c=c/2;
    c=floor(c);
    fprintf('%g',b)
end

lsb=rem(num,2);
msb=b;
   
fprintf(' in binary form.\n\n')
fprintf('The least-significant bit is %g\n\n',lsb)
fprintf('The most-significant bit is %g\n\n\n',msb)



__________________________________________________




I hope that someone is able to help.

P.S. i cant just use the dec2bin command in matlab.

Comments in code would be appreciated.


Cheers

Simon Lee
0
Comment
Question by:fat_mike_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
DRY_GIN earned 300 total points
ID: 8147357
hello,
you are creating integer in decimal format assuming it is binary format - this is wrong
in matlab - long integer can be up to 32 bit's long after that is will be treated as double

anyway, you need to add bits to string , not to integer

here is a working example for you,
it is not optimized at all - but it produces wat you want

clear
clc
num=input('Enter a  positive number: ');
num2=num;
while (num<0)
   clc
   fprintf('Invalid entry!\n');
   a=input('Enter a positive number: ');
end
a=1;
c=num;
d='';
while (c>=1)
   b=rem(c,2);
   c=c/2;
   c=floor(c);
   d=[d char((b)+'0')];
   %a=a*10;
end

lsb=rem(num,2);
msb=b;
   
fprintf('\n\n%g is written %s in binary form.\n\n',num2,d)
fprintf('The least-significant bit is %g\n\n',lsb)
fprintf('The most-significant bit is %g\n\n',msb)
0
 
LVL 3

Expert Comment

by:DRY_GIN
ID: 8147377
typo:
   d=[d char((b)+'0')];

should be changed to :

   d=[char(b+'0') d];
0
 

Expert Comment

by:CleanupPing
ID: 9447111
fat_mike_:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question