Advertisement
Guest User

Decimal to Binary

a guest
Feb 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1.  
  2. function bin = dec2bin2(dec)
  3. i=0;
  4. p=1;
  5. %calculate
  6. while i == 0
  7. if dec/2 > 0
  8. if dec/2 ~= round(dec/2)
  9. bin(p) = 1;
  10. end
  11. if dec/2 == round(dec/2)
  12. bin(p) = 0;
  13. end
  14. end
  15. if dec/2 < 1
  16. bin(p) = 1;
  17. i = 1;
  18. else
  19. p=p+1;
  20. dec = floor(dec/2);
  21. end
  22. end
  23.  
  24. %Invert
  25. newbin=zeros(1,length(bin));
  26. for i = 1:length(bin)
  27. newbin(i) = bin((length(bin)-i)+1);
  28. end
  29.  
  30. %Buffer to 8bits
  31. if length(bin) < 8
  32. bin = [zeros(1,8-length(bin)), newbin];
  33. else
  34. bin = newbin;
  35. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement