Created
June 26, 2017 08:03
-
-
Save glejeune/d975caae85afdaff2f69fc6f7fc62f32 to your computer and use it in GitHub Desktop.
Fun with message...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(message). | |
-export([decode/3, decode/4, encode/1]). | |
decode(M1, M2, M3) -> | |
decode(M1, M2, M3, float_to_integer(length(M1)/8 - 1)). | |
decode(M1, M2, M3, Size) -> | |
[L1, L2, L3] = [[list_to_integer(string:substr(M, I*8+1, 8), 2) || I <- lists:seq(0, Size)] || M <- [M1, M2, M3]], | |
[A bxor B bxor C || {A, B, C} <- lists:zip3(L1, L2, L3)]. | |
encode(Message) -> | |
encode(Message, "", "", ""). | |
encode([], Acc1, Acc2, Acc3) -> | |
io:format("~p~n~p~n~p~n", [Acc1, Acc2, Acc3]), | |
decode(Acc1, Acc2, Acc3, float_to_integer(length(Acc1)/8 - 1)); | |
encode([L|Rest], Acc1, Acc2, Acc3) -> | |
{A, B, C} = reverse(L), | |
encode(Rest, Acc1 ++ A, Acc2 ++ B, Acc3 ++ C). | |
reverse(N) -> | |
A = float_to_integer(N/2), | |
M = N bxor A, | |
B = float_to_integer(M/2), | |
C = M bxor B, | |
N = A bxor B bxor C, | |
{string:right(integer_to_list(A, 2), 8, $0), | |
string:right(integer_to_list(B, 2), 8, $0), | |
string:right(integer_to_list(C, 2), 8, $0)}. | |
float_to_integer(N) -> | |
list_to_integer(float_to_list(N, [{decimals,0}])). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment