9

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

Amro
  • 123,847
  • 25
  • 243
  • 454
Kiran
  • 8,034
  • 36
  • 110
  • 176
  • Possible duplicate of [Decimal to binary as double type array, not string](https://stackoverflow.com/questions/29274368/decimal-to-binary-as-double-type-array-not-string) – Denis Sep 10 '19 at 10:15

4 Answers4

19

You mention not being able to use the function de2bi, which is likely because it is a function in the Communications System Toolbox and you don't have a license for it. Luckily, there are two other functions that you can use that are part of the core MATLAB toolbox: BITGET and DEC2BIN. I generally lean towards using BITGET since DEC2BIN can be significantly slower when converting many values at once. Here's how you would use BITGET:

>> Data = 12;                  %# A decimal number
>> Bin_Vec = bitget(Data,1:6)  %# Get the values for bits 1 through 6

Bin_Vec =

     0     0     1     1     0     0
Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • 1
    Oh, nice! +1 for knowing Matlab well :). Note that I thought the author couldn't use `dec2bin` and had just mistyped. I didn't even know that `de2bi` actually exists. – Jonas Apr 21 '11 at 15:32
  • 1
    +1 for simple to use! However, note many will want to call `fliplr(bitget(Data,1:6))` to get the numbers in the 'correct' order. Depends on usage of course (: – chessofnerd Jun 12 '15 at 15:19
10

A single call to Matlab's built-in function dec2bin can achieve this:

binVec = dec2bin(data, nBits)-'0'
jasxun
  • 571
  • 1
  • 5
  • 11
  • Thanks a lot, I was not aware that dec2bin could be used to get a binary vector like this. – Kiran Apr 26 '11 at 10:28
7

Here's a solution that is reasonably fast:

function out = binary2vector(data,nBits)

powOf2 = 2.^[0:nBits-1];

%# do a tiny bit of error-checking
if data > sum(powOf2)
   error('not enough bits to represent the data')
end

out = false(1,nBits);

ct = nBits;

while data>0
if data >= powOf2(ct)
data = data-powOf2(ct);
out(ct) = true;
end
ct = ct - 1;
end

To use:

out = binary2vector(12,6)
out =
     0     0     1     1     0     0

out = binary2vector(22,6)
out =
     0     1     1     0     1     0
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • @kirancshet: you're welcome. I added a little bit of input testing to avoid an infinite loop. – Jonas Apr 21 '11 at 13:44
2

Are you using this for IEEE 802.11 SIGNAL field? I noticed "length_field" and "16". Anyway here's how I do it.

    function [Ibase2]= Convert10to2(Ibase10,n)

    % Convert the integral part by successive divisions by 2
    Ibase2=[];


    if (Ibase10~=0)

    while (Ibase10>0)

    q=fix(Ibase10/2);
    r=Ibase10-2*q;
    Ibase2=[r Ibase2];
    Ibase10=q;
    end


    else
    Ibase2=0;
    end

    o = length(Ibase2);
    % append redundant zeros
    Ibase2 = [zeros(1,n-o) Ibase2];