1

I have converted a string to binary as follows

message='hello my name is kamran';
messagebin=dec2bin(message);

Is there any method for storing it in array?

2
  • 1
    messagebin is already an array of char. What exactly do you want? Give an example of an output Commented Jun 10, 2012 at 15:55
  • I didn't know you could do dec2bin on a string like that in the first place.. octave for example complains big time. Commented Jun 10, 2012 at 16:01

2 Answers 2

1

I am not really sure of what you want to do here, but if you need to concatenate the rows of the binary representation (which is a matrix of numchars times bits_per_char), this is the code:

message = 'hello my name is kamran';
messagebin = dec2bin(double(message));
linearmessagebin = reshape(messagebin',1,numel(messagebin));

Please note that the double conversion returns your ASCII code. I do not have access to a Matlab installation here, but for example octave complains about the code you provided in the original question.

NOTE

As it was kindly pointed out to me, you have to transpose the messagebin before "serializing" it, in order to have the correct result.

Sign up to request clarification or add additional context in comments.

1 Comment

actually you need to transpose messagebin before linearizing into a vector: reshape(messagebin',1,[]);
1

If you want the result as numeric matrix, try:

>> str = 'hello world';
>> b = dec2bin(double(str),8) - '0'
b =
     0     1     1     0     1     0     0     0
     0     1     1     0     0     1     0     1
     0     1     1     0     1     1     0     0
     0     1     1     0     1     1     0     0
     0     1     1     0     1     1     1     1
     0     0     1     0     0     0     0     0
     0     1     1     1     0     1     1     1
     0     1     1     0     1     1     1     1
     0     1     1     1     0     0     1     0
     0     1     1     0     1     1     0     0
     0     1     1     0     0     1     0     0

Each row corresponds to a character. You can easily reshape it into to sequence of 0,1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.