MATLAB Answers

Vishnu M S

Charecter to binary form

Asked by Vishnu M S
on 30 Jan 2013

I have a character A. I converted this into a binary form(say B). I need to perform some operations using B. While doing this I need to check whether the element B(1)=1 or 0.

For doing so, I write

if B(1)==1

But it doesn't work. It works when I written like this-

if B(1)=='1'

What is the reason? Please tell me...


on 30 Jan 2013

Apparently B is still a character array. Would help if you said how you converted it. Type whos B to see the type.

Jan Simon
on 30 Jan 2013

I do not understand, what the "binary form" of a character is. Please post the corresponding Matlab code to clear the details.


No products are associated with this question.

1 Answer

Answer by Oleg Komarov
on 30 Jan 2013
 Accepted answer

You are comparing the char '1' with the numeric 1.

>> double('1')
ans =

as in the ASCII table.

You have to rescale '1' to 1:

'1'-'0' == 1

and in your case

B(1)-'0' == 1


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!