CHAR AND DOUBLE FUNCTION IN MATLAB

How to see the function coding in matlab that uses char to convert decimal valuein to respective ascii character and uses double to convert character into respective ascii decimal value

답변 (1개)

Guillaume
Guillaume 2014년 10월 1일

0 개 추천

You can't, it's a built-in function of matlab, not an m-file. Most likely it's written in C, and the implementation is trivial in C, it's just a cast.

댓글 수: 6

Raza
Raza 2014년 10월 1일
can i make a function like this if yes then how?
Stephen23
Stephen23 2014년 10월 1일
Errr... write your own C-like language which includes a cast operation.
Guillaume
Guillaume 2014년 10월 1일
Raza, it's not clear what you're asking.
In what language do you want to write this? Matlab obviously already has this.
José-Luis
José-Luis 2014년 10월 1일
편집: José-Luis 2014년 10월 1일
No it is not trivial and it isn't a simple cast. Even in C, you can't just cast
double val = 1.0;
char myString[] = (char) val;
That will never work. You need to call a library function like
printf
sprintf
To cast a double into a string or vice-versa. The internal implementation is not trivial. They are easy to use, but that's a totally different story. If in C++ you can use streams. Or libraries like boost::lexical_cast
Guillaume
Guillaume 2014년 10월 1일
편집: Guillaume 2014년 10월 1일
The OP asked about 'double to convert into respective ascii decimal value', that is in matlab:
d = 65; %is a double in matlab
c = char(d);
That is a simple cast in C:
double d = 65;
char c = (char)d; //c == 'A';
And actually, the reverse doesn't even need a cast. It's just an implicit conversion:
char c = 'A';
double d = c; //d == 65.0
José-Luis
José-Luis 2014년 10월 1일
Maybe. I did not interpret the OP's original question like that though, so we might be arguing over nothing until he clarifies.
It might be good to clarify, as you sort of did, that char is not really an absolute type, as its size can change depending on the implementation. It is an integer in disguise. If you take it like that, then yes, it is trivial (in application if not in implementation).
If not, then my comment remains valid.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2014년 10월 1일

댓글:

2014년 10월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by