What would the code look like to convert a positive decimal (base 10 integer) to its binary form (base 2)?
Show older comments
I am tasked with developing a code that will convert a integer to binary form and display the result. I cannot get my code to work. What would the code look like to perform such an action?
Answers (3)
Birdman
on 17 Jan 2018
You need to use dec2base command. To convert 23 decimal number to its base 2 representation, type
dec2base(23,2)
>> dec2bin(123)
ans = 1111011
If you want DIY code, then you will need mod and a loop.
Walter Roberson
on 17 Jan 2018
Examine:
TheInt = 1736;
bitget(uint32(TheInt), 32:-1:1)
Discard leading 0 values if you want.
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!