- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
How to add padding in 512 bit block?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Hassaan
il 16 Gen 2024
Modificato: Hassaan
il 16 Gen 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 Commenti
Hassaan
il 18 Gen 2024
Append 8 bits i.e 504 + 8 = 512 bits = 64 bytes
Try adjusting total bits and block size as per your requirements.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Interactive Model Editing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!