How to add padding in 512 bit block?

조회 수: 4 (최근 30일)
Ad
Ad 2024년 1월 16일
댓글: Ad 2024년 1월 18일
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)

채택된 답변

Hassaan
Hassaan 2024년 1월 16일
편집: Hassaan 2024년 1월 16일
% 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
Block 1: 1 1 0 1 0 1 0 1 1 0 1 1 0 1 1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 1 0 0 1 0 0 1 1 0 1 1 0 0 1 1 1 0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 0 0 0 1 1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 1 0 0 1 1 0 1 1 1 1 0 0 1 1 1 0 0 1 0 1 0 0 0 1 0 1 1 1 0 1 0 1 0 0 1 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 0 0 1 0 1 1 1 1 0 0 1 1 0 1 0 0 1 1 1 1 1 0 0 0 1 0 0 1 0 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 1 1 1 0 0 0 1 1 0 0 1 1 1 1 0 1 1 0 1 0 0 1 0 1 0 1 0 1 1 1 1 1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 0 1 0 1 1 0 0 0 1 0 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 1 1 1 1 0 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 1 0 0 0 0 0 1 0 1 1 1 0 0 1 0 0 1 0 0 1 1 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 1 1 0 1 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 1 0 1 0 0 0 1 0 1 0 0 1 1 0 1 0 0 1 1 0 1 0 1 0 1 0 1 0
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
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  댓글 수: 3
Hassaan
Hassaan 2024년 1월 18일
Append 8 bits i.e 504 + 8 = 512 bits = 64 bytes
Try adjusting total bits and block size as per your requirements.
Ad
Ad 2024년 1월 18일
i am grateful however what if we are asking to deal with byte object where byte is the class and its object is data block and then you need to add padding to that block

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Model Editing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by