I need to convert a Decimal Number to a Binary Vector
For example, Something like this:
length=de2bi(length_field,16);
Unfortunately, because of licensing, I cannot use this command. Is there any quick short technique for converting binary to a vector.
Here is what I am looking for,
If
Data=12;
Bin_Vec=Binary_To_Vector(Data,6) should return me
Bin_Vec=[0 0 1 1 0 0]
Thanks
An easy method of converting decimal to binary number equivalents is to write down the decimal number and to continually divide-by-2 (two) to give a result and a remainder of either a “1” or a “0” until the final result equals zero. So for example. Convert the decimal number 29410 into its binary number equivalent.
str = dec2bin( d ) returns the binary representation of symbolic number d as a character vector. d must be a nonnegative integer. If d is a matrix or multidimensional array of symbolic numbers with N elements, dec2bin returns a character array with N rows.
A single call to Matlab's built-in function dec2bin
can achieve this:
binVec = dec2bin(data, nBits)-'0'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With