Reverse operation of horzcat, reshape and dec2bin code I have written.
3 views (last 30 days)
Show older comments
darren Clipson
on 26 Jun 2021
Edited: Walter Roberson
on 6 Jul 2021
Hello, I have been writing a very simple way of encoding text as part of a level 5 course I am undertaking currently. I have reached a point where I am happy with the encoded data but I have become a bit of a car crash trying to reverse the operation. The trouble is; I am a complete novice on both code and Matlab in general. My course is a communications course and not really code focused. I chose this as a project as I found is really interesting but I do seem to have underrestimated the complexity of the task somewhat.
The elements I am struggling with are:
chr = ( secret_message );
group_len = 8;
len = numel( chr );
d = rem( len, group_len );
chr = horzcat( chr, repmat( char(255), 1, group_len-d ) );
chr = reshape( chr, 8,[] );
secret_message3 = chr(:,1:195)
secret_messageF = chr(1:length(secret_message3))
binX = dec2bin(secret_messageF,8)
This takes an already encoded message and rearranges the layout before changing to binary.
Any help is greatly appreciated. I need to be able to get back to the top of this code at the ('secret_message') in its original format, which was ASCII.
Kind Regards
3 Comments
DGM
on 27 Jun 2021
In order to know how to reverse (e.g. reshape) operations, one needs to know what they're trying to undo. As you said yourself, the prior code could be anything. It should stand to reason then that so is the current status of any possible answer.
If you'd rather avoid revealing your code, but need to understand how to reverse the transformation, you might try to come up with a minimal working example that is succinct and generalized enough that you're comfortable sharing it.
Accepted Answer
Walter Roberson
on 27 Jun 2021
chr = reshape( chr, 8,[] );
chr will be 8 by something
secret_message3 = chr(:,1:195)
secret_message3 will be 8 by 195 (or an error if chr was not at least 195 columns)
secret_messageF = chr(1:length(secret_message3))
length(X) is defined as:
SZ = size(X);
if any(SZ == 0)
LENGTH = 0
else
LENGTH = max(SZ)
end
so it is 0 if the array is empty in any dimension, and otherwise is the longest dimension no matter which one that is .
With secret_messageF being 8 x 195, the longest dimension is 195, so length(secret_messageF) is 195.
So you are taking chr(1:195) which is indexing a 2D array with a single dimension. That operation is well defined in MATLAB, and means that you will get the first 195 consecutive memory locations out of the array. Arrays are stored "down" first, so A(1,1) then A(2,1) then A(3,1), A(4,1), A(5,1), A(6,1), A(7,1), A(8,1), A(1,2), A(2,2), A(3,2) and so on. chr has 8 rows, so the first 195 entries in memory is the first 24 full columns plus another 3 from the 25th column.
It seems pretty unlikely that that would be what you would want.
More Answers (1)
darren Clipson
on 27 Jun 2021
Edited: darren Clipson
on 6 Jul 2021
10 Comments
Walter Roberson
on 3 Jul 2021
I suggest you have a look at https://www.mathworks.com/matlabcentral/answers/466636-caesar-cyphor-encryption-problem?s_tid=srchtitle
See Also
Categories
Find more on Characters and Strings 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!