フィルターのクリア

How to convert decimal into binary?

43 ビュー (過去 30 日間)
Sky Scrapper
Sky Scrapper 2019 年 1 月 22 日
編集済み: Jan 2021 年 11 月 1 日
Hello,
I need to convert n-bit decimal into 2^n bit binary number. I do not have much idea. Can anybody help me please?
  4 件のコメント
Jan
Jan 2019 年 1 月 22 日
2^8 or 2^8-1 ?
Stephen23
Stephen23 2019 年 1 月 22 日
編集済み: Stephen23 2019 年 1 月 22 日
Get rid of the loop:
>> V = 0:pow2(8)-1;
>> dec2bin(V)
ans =
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
... lots of rows here
11111010
11111011
11111100
11111101
11111110
11111111

サインインしてコメントする。

採用された回答

Jan
Jan 2019 年 1 月 22 日
編集済み: Jan 2021 年 11 月 1 日
This code shows '11111111' only, because you overwrite the output in each iteration:
n= 8;
for i = 0:2^n-1
x = dec2bin(i,8);
end
Therefore x contains the last value only: dec2bin(2^n-1, 8).
Better:
x = dec2bin(0:2^n-1, 8);
Or if you really want a loop:
n = 8;
x = repmat(' ', 2^n-1, 8); % Pre-allocate
for i = 0:2^n-1
x(i+1, :) = dec2bin(i,8);
end
x
[EDITED] If you want the numbers 0 and 1 instead of a char matrix with '0' and '1', either subtract '0':
x = dec2bin(0:2^n-1, 8) - '0';
But to avoid the conversion to a char and back again, you can write an easy function also:
function B = Dec2BinNumeric(D, N)
B = rem(floor(D(:) ./ bitshift(1, N-1:-1:0)), 2);
end
% [EDITED] pow2(n) reülaced by faster bitshift(1, n)
PS. You see, the underlying maths is not complicated.
  9 件のコメント
Walter Roberson
Walter Roberson 2019 年 1 月 23 日
A one-million bit binary number cannot be converted to a double precision value.
Jan
Jan 2019 年 1 月 23 日
If
dec2bin(0:2^n-1, 8) - '0'
is working, calling
Dec2BinNumeric(0:2^n-1, 8)
is not a serious difference.

サインインしてコメントする。

その他の回答 (2 件)

PRAVEEN GUPTA
PRAVEEN GUPTA 2019 年 7 月 8 日
i have string of number [240 25 32 32]
i want to convert them in binary
how can i do this???
  2 件のコメント
Jan
Jan 2019 年 7 月 8 日
Do no attach a new question as an asnwer of another one.
Did you read this thread? dec2bin has been suggested already, as well as a hand made solution Dec2BinNumeric. Simply use them.
AB WAHEED LONE
AB WAHEED LONE 2021 年 3 月 6 日
編集済み: AB WAHEED LONE 2021 年 3 月 6 日
I know it is late but somwhow it may help
bin_array=dec2bin(array,8)-'0';

サインインしてコメントする。


vandana Ananthagiri
vandana Ananthagiri 2020 年 2 月 5 日
function A = binary_numbers(n)
A = double(dec2bin(0:((2^n)-1),n))-48;
end
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 2 月 5 日
Why 48?
I know the answer, but other people reading your code might not, so I would recommend either a comment or a different representation.
vincent voogt
vincent voogt 2021 年 11 月 1 日
Maybe a late reply, but dec2bin return as string of ASCII characters, where 0-9 are mapped on character number 48-57.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by