How to create block size of 512 bit from binary data and add padding to that block?

7 views (last 30 days)
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?

Answers (1)

Hassaan
Hassaan on 19 Jan 2024
Edited: Hassaan on 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
Block 1: 69 246 200 51 46 156 20 79 10 190 128 236 146 70 150 120 123 12 41 38 172 49 16 11 127 52 129 227 206 135 123 19 206 112 130 204 2 41 162 130 139 170 83 143 13 80 193 234 99 19 144 143 195 109 40 59 214 136 40 184 237 46 65 245 Block 2: 76 188 236 252 75 72 233 63 20 246 29 113 173 246 52 1 246 113 58 254 28 76 50 16 225 20 4 61 250 118 56 166 30 247 254 18 176 243 98 56 177 59 35 29 74 174 196 219 70 64 12 117 190 145 101 102 189 31 175 35 9 0 0 0
% 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
Padded Block 2: 76 188 236 252 75 72 233 63 20 246 29 113 173 246 52 1 246 113 58 254 28 76 50 16 225 20 4 61 250 118 56 166 30 247 254 18 176 243 98 56 177 59 35 29 74 174 196 219 70 64 12 117 190 145 101 102 189 31 175 35 9 0 0 0
% % '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
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
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.

Categories

Find more on Startup and Shutdown 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!