- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
How to add padding in 512 bit block?
2 views (last 30 days)
Show older comments
I want to add and delete extra bits in 512 blocks, no matter what extra numbers of bit i give. (user will provide the no. of bits and bits itself)
0 Comments
Accepted Answer
Hassaan
on 16 Jan 2024
Edited: Hassaan
on 16 Jan 2024
% Generate dummy binary data (2000 random bits)
totalBits = 2000;
blockSize = 512; % Size of each block in bits
binaryData = randi([0 1], 1, totalBits);
% User Input for Extra Bits
% Example: Adding 10 extra bits
extraBits = [1 0 1 0 1 0 1 0 1 0]; % User-defined extra bits
addExtraBits = true; % Set to false if you don't want to add extra bits
% Append Extra Bits to Data
if addExtraBits
binaryData = [binaryData extraBits];
end
% Calculate the number of full blocks
numFullBlocks = floor(length(binaryData) / blockSize);
% Initialize a cell array to store the blocks
blocks = cell(1, numFullBlocks);
% Extract each block
for i = 1:numFullBlocks
startIndex = (i - 1) * blockSize + 1;
endIndex = i * blockSize;
blocks{i} = binaryData(startIndex:endIndex);
end
% Check if there's any remaining data after the full blocks
remainingDataLength = mod(length(binaryData), blockSize);
if remainingDataLength > 0
remainingData = binaryData(end - remainingDataLength + 1:end);
% Add the remaining data as the last block (if needed)
blocks{end + 1} = remainingData;
end
% Option to Remove Extra Bits
removeExtraBits = false; % Set to true if you want to remove extra bits
if removeExtraBits && addExtraBits
% Remove the extra bits from the last block
blocks{end} = blocks{end}(1:end-length(extraBits));
end
% Display the blocks (optional)
for i = 1:length(blocks)
disp(['Block ' num2str(i) ': ' num2str(blocks{i})]);
end
This script defines the padTo512Bits function and then demonstrates its use with an example binary string '110100101'. The result is then displayed using the disp function.
-----------------------------------------------------------------------------------------------------------------------------------------------------
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
Feel free to contact me.
3 Comments
Hassaan
on 18 Jan 2024
Append 8 bits i.e 504 + 8 = 512 bits = 64 bytes
Try adjusting total bits and block size as per your requirements.
More Answers (0)
See Also
Categories
Find more on Interactive Model Editing 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!