See i have a binary code of unknown length , i want to divide it into blocks of length four and assign each block to a variable .

4 Kommentare

How is your "binary code of unknown length" stored?
Is it stored like
'0001101000111'
is it stored like
[0 0 0 1 1 0 1 0 0 0 1 1 1]
is it stored like
[false false false true true false true faslse false false true true true]
is it encoded into a single uint64 number (and so has a maximum length of 64) ?
Walter Roberson
Walter Roberson am 22 Mai 2023
and assign each block to a variable
Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.
Ganesh
Ganesh am 23 Mai 2023
It is stored as '101011100'.
It's ike a message that is converted into bits from text or voice or may be image.
Yes, it is encoded into single uint64 number
Walter Roberson
Walter Roberson am 23 Mai 2023
What do you want to have happen if the character vector is not an exact multiple of 4 long?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Abhishek
Abhishek am 22 Mai 2023

0 Stimmen

Hi Ganesh,
It is my understanding that you are unable to divide a binary data of unknown length into multiple blocks of length four.
To resolve this issue, you may perform bit operations on the binary data to extract last four bits, then store the result in an array and assign each block to a variable.
For your reference, the below example demonstrates a simple approach to divide a binary data of unknown length to multiple blocks:
% register is the variable that stores binary number.
register = 0b1001011010001010101011010101010110000010101111110101010101;
% An Array to store the binary number of length of four.
blockOfLengthFour = [];
% Simple bit operation to extract last four bits and append them to an
% array
while register
% Get the last four bits
nextFourBits = bitand(register, 15);
% Append the result to final result array
blockOfLengthFour = [nextFourBits, blockOfLengthFour];
% remove last four bits of original binary number
register = bitshift(register, -4);
end
blockOfLengthFour
blockOfLengthFour = 1×15
2 5 10 2 10 11 5 5 6 0 10 15 13 5 5

6 Kommentare

Ganesh
Ganesh am 22 Mai 2023
what if the length is provided at the beginning
Ganesh
Ganesh am 22 Mai 2023
and in the above answer i want a variable assigned to each block, like an array .
Ex: message[1],message[2]..... instead of assigning it to a single variable
Abhishek
Abhishek am 22 Mai 2023
Bearbeitet: Abhishek am 22 Mai 2023
If length is provided, use a counter to compare it with the given length and update the while loop condition accordingly.
Now, addressing the second query, you can access the array variable "blockOfLengthFour" using indices such as blockOfLengthFour(1), blockOfLengthFour(2), and so on.
Ganesh
Ganesh am 22 Mai 2023
while accessing i am getting only single bit
Abhishek
Abhishek am 22 Mai 2023
The last line of provided code gives 15 different values and each of them is greater than single bit.
Ganesh
Ganesh am 23 Mai 2023
Tqs for the answer.
Hey I have another question , after dividing it into blocks i have to assign each block a degree pseudorandomly and based on the degree assigned i have to generate a linear equaton (the coefficients in equation are blocks of message generated ) for each block.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by