Please can somebody expain this code.

4 views (last 30 days)
Syed Bilal
Syed Bilal on 17 Mar 2014
Commented: Walter Roberson on 3 Feb 2025
tic
M = 'FFFFFFFFFFFFFFFF';
MB=[];
for i=1:16
Mi=M(i);
MBi=['0000',dec2bin(hex2dec(Mi))];
MBi=MBi(end-3:end);
MBi=[str2num(MBi(1)),str2num(MBi(2)),str2num(MBi(3)),str2num(MBi(4))];
end
M=MB
  1 Comment
Kevin Claytor
Kevin Claytor on 17 Mar 2014
Did you have a look at the docs:
They pretty much explain it. If you still don't understand the whole thing, post back with what you do understand and where you're getting stuck. You might also find;
helpful.
I'm disappointed that I can't add the the link markup into code segments. It would have been more fun that way.

Sign in to comment.

Answers (1)

Ishaan
Ishaan on 29 Jan 2025
The code you shared appears to convert a 16-character hexadecimal string into its equivalent binary representation.
Refer to the following code with added comments and some improvements for better understanding.
tic % Start a timer to measure the execution time of the code
M = 'FFFFFFFFFFFFFFFF'; % Initialize a string representing a 16-character hexadecimal number
MB = []; % Initialize an empty array to store binary representations
for i = 1:16 % Loop through each character in the hexadecimal string
Mi = M(i); % Extract the i-th character from the hexadecimal string M
% Convert the hexadecimal character to a decimal number, then to a binary string
MBi = ['0000', dec2bin(hex2dec(Mi))];
% Ensure the binary string is 4 characters long by taking the last 4 characters
MBi = MBi(end-3:end);
% Convert each character in the binary string to a number and store in MBi
MBi = [str2double(MBi(1)), str2double(MBi(2)), str2double(MBi(3)), str2double(MBi(4))];
MB = [MB, MBi]; % Append the binary array to MB
end
M = MB % Assign the final binary representation to M
M = 1×64
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
toc; % End the timer and report the execution time
Elapsed time is 0.131393 seconds.
I noticed 3 potential errors in the code you provided and made these changes.
  1. The result at each intermediate step is not stored and is overridden. Added the line “MB = [MB, MBi];” at the end of the loop to that.
  2. ‘str2num` is not recommended in this case as it is not suitable for converting single character strings to numbers. Instead, you should use str2double or directly convert the character to a number using -'0' which works well for character arrays representing digits.
  3. “tic” was used without “toc”. “tic” marks the start of the timer and “toc” marks its end reporting/returning the time it took for the code to execute between the two. I added "toc" at the end of the code.
I hope that it is now clear what the code snippet you provided does.
  2 Comments
Walter Roberson
Walter Roberson on 3 Feb 2025
dec2bin(hex2dec(Mi),4)
would make more sense then the string manipulation you are doing.
Walter Roberson
Walter Roberson on 3 Feb 2025
Instead of
MBi = [str2double(MBi(1)), str2double(MBi(2)), str2double(MBi(3)), str2double(MBi(4))];
you can use
MBi = MBi - '0';

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!