How to convert a number in to hexa decimal in Simulink without using functions?

Hi everyone,
I am trying to convert a number in to hexa decimal in Simulink without using any functions. Is there any way that this can be done with existing Simulink block? or else can anyone suggest me what would be the best way to do it in Simulink?
Thanks in Advance

1 Kommentar

Integer or floating-point numbers? Signed or unsigned? Range of values?
There are techniques that are workable (if slightly tedious) for integers 0 to 255 that get more tiresome as you increase the range.
Heck, for small integers I would probably just use a pair of lookup tables.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Samatha Aleti
Samatha Aleti am 24 Feb. 2020
Hi,
You may use "MATLAB function block" in your SIMULINK model to convert a number into hexadecimal format using “dec2hex" function.
Refer the sample SIMULINK model attached.

6 Kommentare

@Samatha Aleti Thanks for your reply. I tried to use a Matlab Function, but I am getting an error message which says data 'y' from the function block is inferred as a variable size matrix, but its properties in Model Explorer specify its size as inherited or fixed. Morever I am using Matlab R2015b and R2018b, so I can't open your Simulink file. It would be very much helpful, if you can convert and resend the Simulink file in the above versions
Sharing the screenshot of the sample model and the MATLAB code:
function y = fcn(u)
% "dec2hex" returns character vector
y = uint8(dec2hex(u, 16) - '0'); % Type converion
end
@Samatha Aleti Once again thanks for your quick response. I inserted your code in Matlab Function and passed a constant value as an input argument to the function, but I am getting Size mismatch error
Size mismatch (size [:8 x 1] ~= size [1 x 16]).
The size to the left is the size of the left-hand side of the assignment.
Function 'MATLAB Function' (#1730.31.32), line 4, column 1:
"y"
Launch diagnostic report.
and Morever what will be the output of the function. For example if I want to convert "123" in to hexa decimal, then will the function output 7B or something else, because when I executed the above command in command window with 123 as u, it displayed(0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 18). I read in documentation that Simulink won't allow char and strings as signals.
Subtracting '0' from the dec2hex is not likely to generate useful results. For '0' to '9' it will generate 0 through 9, which could be possibly useful. However for 'A' that would be decimal 65 and subtract '0' which decimal 48, you would get decimal 17 which is not useful.
Question: are you looking for "nibble" resule, converting say 130 hex 0x12 into the binary values 1 and 2? Or are you looking for character encoding like '12' for that value?
The 7B you mention, do you want '7' 'B' or do you want uint8([7 11])?
What range of values do you have? For values beyond 255 you start needing to worry about byte order.
Converting uint8 to nibbles is easy using Math blocks
@Walter Roberson Thanks for your reply. I have 8 individual bytes. I need to convert them in to hexa decimal and concatenate. So my values lies in between 0 and 255 and yes, I am looking for nibble, as you mentioned above and also could you tell me, how do you convert uint8 in to nibble? and which block are you using?
Use two math blocks. The first one is uint8(floor(double(x)/16)) . The second is uint8(mod(x, 16)) . You can do the same thing for each of the 8 bytes, and put the resulting signals into a block that puts them together into a vector of whatever order you want.
Math blocks can be optimized inline by the Simulink accelerator so this can be efficient.
If you have a vector of 8 bytes already then extracting the nibbles can be done in vectorized form. Then it just becomes a matter of splicing the two vectors together, which can be done by concatenation and reshape.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Simulink finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by