How to apply dec2bin on a string to get a single output?

3 views (last 30 days)
Ammy
Ammy on 8 Mar 2022
Edited: Arif Hoq on 8 Mar 2022
I want to convert decimal into binary .
Since my data is in string like
a = '56515219790691171413109057904011688695424810155802929973526481321309856242040'
I have tried dec2bin(a), but it gives output in different rows,
I need the following output
dec2bin(56515219790691171413109057904011688695424810155802929973526481321309856242040)
ans='111110011110010011110110001100010001101000000110101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
But I'm not getting the same output by dec2bin(a).
Furthermore, I have to convert all the rows in to binary by importing a .txt file. Any help is much appreciated.
Thanking in anticipation.

Accepted Answer

Arif Hoq
Arif Hoq on 8 Mar 2022
Edited: Arif Hoq on 8 Mar 2022
a = '56515219790691171413109057904011688695424810155802929973526481321309856242040';
a1=cell2mat(split(a,' '));
a2=str2double(a1);
% check=dec2bin(5,6)
out=dec2bin(a2)
out = '111110011110010011110110001100010001101000000110101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
writematrix(out,'sampletable.txt')
  7 Comments
Arif Hoq
Arif Hoq on 8 Mar 2022
stephen, you are right. i was in a hurry while coding. i just checked the result not the coding size. Thank you very much for the hints.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!