Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
53
Task/Bitwise-IO/Erlang/bitwise-io-1.erl
Normal file
53
Task/Bitwise-IO/Erlang/bitwise-io-1.erl
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
-module(bitwise_io).
|
||||
-compile([export_all]).
|
||||
|
||||
%% Erlang allows for easy manipulation of bitstrings. Here I'll
|
||||
%% present a function that can take a message of 8-bit ASCII
|
||||
%% characters and remove the MSB, leaving the same message in 7-bits.
|
||||
|
||||
compress(Message) ->
|
||||
<< <<X:7>> || <<X:8>> <= Message >>.
|
||||
|
||||
%% Here we decompress the message.
|
||||
|
||||
decompress(Message) ->
|
||||
<< <<X:8>> || <<X:7>> <= Message >>.
|
||||
|
||||
%% Now a test to demonstrate that this conversion works:
|
||||
|
||||
test_bitstring_conversion() ->
|
||||
Message = <<"Hello, Rosetta Code!">>,
|
||||
io:format("~p: ~B~n",[Message, bit_size(Message)]),
|
||||
Compressed = compress(Message),
|
||||
io:format("~p: ~B~n",[Compressed, bit_size(Compressed)]),
|
||||
Decompressed = decompress(Compressed),
|
||||
io:format("~p: ~B~n",[Decompressed, bit_size(Decompressed)]),
|
||||
io:format("~p = ~p ? ~p~n",[Message, Decompressed,
|
||||
Message =:= Decompressed]).
|
||||
|
||||
%% Now to show this on file output, we'll write the compressed version
|
||||
%% to a file. Now, erlang's file:write_file expects objects that are
|
||||
%% multiples of 8bits. We'll add padding to allow the writing to
|
||||
%% complete, and then discard the padding when reading the file back
|
||||
%% in.
|
||||
|
||||
test_file_io() ->
|
||||
Message = <<"Hello, Rosetta Code!">>,
|
||||
FileName = "bitwise_io.dat",
|
||||
Compressed = compress(Message),
|
||||
PaddingSize = (8 - (bit_size(Compressed) rem 8)) rem 8,
|
||||
PaddedCompressed = <<Compressed:(bit_size(Compressed))/bitstring,
|
||||
0:PaddingSize>>,
|
||||
file:write_file(FileName,PaddedCompressed),
|
||||
{ok, ReadIn} = file:read_file(FileName),
|
||||
UnpaddedSize = bit_size(ReadIn) - 7,
|
||||
Unpadded =
|
||||
case UnpaddedSize rem 7 =:= 0 of
|
||||
true ->
|
||||
<<T:(UnpaddedSize)/bitstring,_:7>> = ReadIn,
|
||||
T;
|
||||
false ->
|
||||
<< <<X:7>> || <<X:7>> <= ReadIn >>
|
||||
end,
|
||||
Decompressed = decompress(Unpadded),
|
||||
io:format("~p~n",[Decompressed]).
|
||||
8
Task/Bitwise-IO/Erlang/bitwise-io-2.erl
Normal file
8
Task/Bitwise-IO/Erlang/bitwise-io-2.erl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
184> bitwise_io:test_bitstring_conversion().
|
||||
<<"Hello, Rosetta Code!">>: 160
|
||||
<<145,151,102,205,235,16,82,223,207,47,78,152,80,67,223,147,42,1:4>>: 140
|
||||
<<"Hello, Rosetta Code!">>: 160
|
||||
<<"Hello, Rosetta Code!">> = <<"Hello, Rosetta Code!">> ? true
|
||||
ok
|
||||
185> bitwise_io:test_file_io().
|
||||
<<"Hello, Rosetta Code!">>
|
||||
Loading…
Add table
Add a link
Reference in a new issue