A bug of build-in function Char?

조회 수: 1 (최근 30일)
yang
yang 2012년 12월 20일
Well, I have a double array readed from a binary file, such as X=[66 43 22 0 67],
When I convert the array into string, char(X), the ans is 'B+! C'
I think that the function Char() should take [0] as the ending of a string, why it treat zero also as a part of string?

채택된 답변

Jonathan Sullivan
Jonathan Sullivan 2012년 12월 20일
This is where MATLAB differs from C. C will assume that 0 is the end of the string. MATLAB has explicit start and end points of the array.
If you want that functionality, you'll have to do something like:
X = [66 43 22 0 67];
char(X(1:find(X == 0,1)))

추가 답변 (2개)

yang
yang 2012년 12월 20일
Ok, fine, thanks.

Walter Roberson
Walter Roberson 2012년 12월 21일
sprintf('%s', char(X))
sprintf() has an undocumented effect (some would say it is a bug) of ending at the first binary 0.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by