- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
How to create block size of 512 bit from binary data and add padding to that block?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
What if we are asking to deal with byte object where bytes is the class and its object is data block and then you need to add padding to that block?
0 Kommentare
Antworten (1)
Hassaan
am 19 Jan. 2024
Bearbeitet: Hassaan
am 19 Jan. 2024
% Example binary data as a logical array
totalBits = 1000; % Example total bits
binaryData = logical(randi([0 1], 1, totalBits));
% Convert binary data to bytes. This assumes each byte is represented as a uint8.
numBytes = ceil(length(binaryData) / 8);
byteData = uint8(zeros(1, numBytes)); % Initialize array for byte data
for i = 1:numBytes
% Convert each set of 8 bits to the corresponding byte
fromBit = (i - 1) * 8 + 1;
toBit = min(fromBit + 7, length(binaryData));
byteData(i) = bi2de(binaryData(fromBit:toBit), 'left-msb');
end
% Determine the number of full blocks and the size of the final block
numFullBlocks = floor(numBytes / 64);
finalBlockSize = rem(numBytes, 64);
% If the final block is not full, pad it with zeros
if finalBlockSize > 0
paddingNeeded = 64 - finalBlockSize;
byteData = [byteData, uint8(zeros(1, paddingNeeded))];
numFullBlocks = numFullBlocks + 1; % Increment block count since we now have an additional padded block
end
% Split the byte data into 64-byte blocks
blocks = reshape(byteData, [64, numFullBlocks]);
% Now print each block in a single line without commas
for i = 1:numFullBlocks
% Convert block to string and concatenate numbers with spaces
blockStr = sprintf('%d ', blocks(:, i));
% Print block information
disp(['Block ' num2str(i) ': ' blockStr])
end
% If there is a padded block, print it as well
if finalBlockSize > 0
% Convert padded block to string and concatenate numbers with spaces
paddedBlockStr = sprintf('%d ', blocks(:, end));
% Print padded block information
disp(['Padded Block ' num2str(numFullBlocks) ': ' paddedBlockStr])
end
% % 'blocks' is now a 2D array where each column represents a block
% % Now print each block
% for i = 1:numFullBlocks
% disp(['Block ' num2str(i) ': '])
% disp(blocks(:, i)') % Transpose the block for display purposes
% end
%
% % If there is a padded block, print it as well
% if finalBlockSize > 0
% disp(['Padded Block ' num2str(numFullBlocks) ': '])
% disp(blocks(:, end)') % Transpose the block for display purposes
% end
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!