How to convert decimal into hexadecimal in "S function" Matlab ?

조회 수: 1 (최근 30일)
Md Rabiul Islam
Md Rabiul Islam 2021년 7월 5일
편집: Jonas 2021년 7월 7일
Hello,
I am using Matlab 2021a. In simulink, I am giving input value ( 2748) and expecting output value in hexadecimal. As we know that simulink is not supporting "character", I am generating output in UINT8. But my requirement is to produce :
Input : UINT16 ( 2748)
output : UINT8
y1[0] = 10 ( decimal conversion of "0A")
y1[2] = 188 ( decimal conversion of "BC")
I have written below code for conversion :
z[0] = *u0; // z is a working vector with data type UINT16 and u0 is an input
z[1] = 16;
z[2] = fmod(z[0],16);
z[3] = (z[0]-z[2])/z[1];
z[4] = fmod(z[3],z[1]);
z[5] = (z[3]- z[4])/ z[1];
z[6] = fmod (z[5], z[1]);
z[7] = (z[5]- z[6])/ z[1];
y1[0]= z[7]; // giving output = 0 // y is output with data type UINT8
y1[1]= z[5]; // giving output = 10
y1[2]= z[4]; // giving output = 11
y1[3]= z[2]; // giving output = 12
But I need output -
y1[0] = decimal conversion of first two bytes = 00 10 > 10
y1[1] = decimal conversion of first two bytes = 11 12 > 188
I also have attached the simulink block and output. Can I ask for recommendation how can I write code to generate such output ?

답변 (2개)

dpb
dpb 2021년 7월 5일
편집: dpb 2021년 7월 5일
>> uint8(hex2dec(reshape(strrep(sprintf('%#X',2748),'X',''),2,[]).'))
ans =
2×1 uint8 column vector
10
188
>>
  댓글 수: 2
Md Rabiul Islam
Md Rabiul Islam 2021년 7월 7일
hex2dec or dec2hex is not accepted in "S- function". If I use this function, it shows error.
dpb
dpb 2021년 7월 7일
That's bizarre. Why in the world would that not be acceptable, but I guess it is what it is...one workaround is
>> txt=cellstr(reshape(strrep(sprintf('%#X',2748),'X',''),2,[]).');
>> uint8(cellfun(@(c)sscanf(c,'%x'),txt))
ans =
2×1 uint8 column vector
10
188
>>

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


Jonas
Jonas 2021년 7월 7일
편집: Jonas 2021년 7월 7일
input = uint16(2748)
input = uint16 2748
output = zeros(2,1,'uint8');
output(1) = bitshift(input,-8);
output(2) = bitand(input,255);
output
output = 2×1
10 188
Hexadecimal is just a way of displaying, not a data type.

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by