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

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

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 I want to apply the above in the file, please by importing it.
I Have tried;
Let A be the data imported
a1=cell2mat(split(A,' '));
a2=str2double(a1);
out=dec2bin(a2)
But not geeting it
This code contains many superfluous steps.
Note that SPLIT does not split any text, it just places the character vector in a scalar cell:
a = '56515219790691171413109057904011688695424810155802929973526481321309856242040';
c = split(a,' ')
c = 1×1 cell array
{'56515219790691171413109057904011688695424810155802929973526481321309856242040'}
after that the CELL2MAT simply returns exactly the same character vector:
a1 = cell2mat(c)
a1 = '56515219790691171413109057904011688695424810155802929973526481321309856242040'
isequal(a,a1)
ans = logical
1
So there is no point in any of that code, it literally does nothing.
The entire answer can be replaced by this much simpler code, which gives exactly the same output:
out = dec2bin(str2double(a))
out = '111110011110010011110110001100010001101000000110101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
@Stephen Thank you very much for your valuable comment, may I please request you how can I apply this command on the above attached file, I want to convert each row of above attached file into binary. Also how can I vote your answer as there is no option with your comment.
try this code for your attachment.
A=readtable('Test.txt','ReadVariableNames', false);
Output=dec2bin(table2array(A))
Output = 20×256 char array
'0111110011110010011110110001100010001101000000110101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '1110001001010011010010100011010100110010110100001001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0110001011011001011101111001110110111110111010011011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0111011010101001010011010001001110001010011010110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0010001101110111110001111101011010010000101000100100001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0000101000000110010000111111101110001111110011000001010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '1010111000111111011111011011101000001011110111101000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0011010010100010110101001010001110110000000010010001011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0000010011000100100100000101001010001011111001110101101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0001011010010100100110110111001010000111110101001111010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '1011000000011010011001111111011100010110010001110110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '1110011100010110101011101101001011001111000001101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0101101001010111110000111110001101010100100000100000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0101110011101001011001010000010111101110111101110010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0111000110100101101111010010011111000110001001011100110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '1010000000011000001101100110111101001110100100011110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '1100110101111100111001100101110101110000011111011010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0000111101011111000010101110101000001001100101100011010010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0100011100011010110100001010100011010101011111110101100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' '0000111011000111001110001000010100010100000111111110010110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
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)

Categories

Asked:

on 8 Mar 2022

Edited:

on 8 Mar 2022

Community Treasure Hunt

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

Start Hunting!