Bitwise IO: Difference between revisions

m
Imlementation in Red programming language
(clarify that most significant bit is read first)
m (Imlementation in Red programming language)
Line 2,435:
<pre>
Decrunched string equal to original.
</pre>
 
=={{header|Red}}==
<lang red>Red [
Title: "Bitwise IO"
Link: http://rosettacode.org/wiki/Bitwise_IO
Source: https://github.com/vazub/rosetta-red
File: "%bitwiseio.red"
Rights: "Copyright (C) 2020 Vasyl Zubko. All rights reserved."
License: "Blue Oak Model License - https://blueoakcouncil.org/license/1.0.0"
Tabs: 4
]
 
str-compress: function [
"Compressesor"
str [string!]
][
buf: copy ""
bit-str: enbase/base str 2
foreach [bit1 bit2 bit3 bit4 bit5 bit6 bit7 bit8] bit-str [
append buf rejoin [bit2 bit3 bit4 bit5 bit6 bit7 bit8]
]
if (pad-bits: (length? buf) // 8) <> 0 [
loop (8 - pad-bits) [append buf "0"]
]
debase/base buf 2
]
 
str-expand: function [
"Decompressor"
bin-hex [binary!]
][
bit-str: enbase/base bin-hex 2
filled: 0
buf: copy []
acc: copy ""
foreach bit bit-str [
append acc bit
filled: filled + 1
if filled = 7 [
append buf debase/base rejoin ["0" acc] 2
clear acc
filled: 0
]
]
if (last buf) = #{00} [take/last buf]
rejoin buf
]
 
; DEMO
in-string: "Red forever!"
compressed: str-compress in-string
expanded: str-expand compressed
prin [
pad "Input (string): " 20 mold in-string newline newline
pad "Input (bits): " 20 enbase/base in-string 2 newline
pad "Compressed (bits): " 20 enbase/base compressed 2 newline newline
pad "Input (hex): " 20 to-binary in-string newline
pad "Compressed (hex): " 20 compressed newline newline
pad "Expanded (string): " 20 mold to-string expanded
]
</lang>
 
{{out}}
<pre>
Input (string): "Red forever!"
 
Input (bits): 010100100110010101100100001000000110011001101111011100100110010101110110011001010111001000100001
Compressed (bits): 1010010110010111001000100000110011011011111110010110010111101101100101111001001000010000
 
Input (hex): #{52656420666F726576657221}
Compressed (hex): #{A597220CDBF965ED979210}
 
Expanded (string): "Red forever!"
</pre>
 
Anonymous user