From f1fd3c1dea1b578714acf3dad6f02a0d2ec7ce9d Mon Sep 17 00:00:00 2001 From: Arity-T Date: Sun, 17 Nov 2024 20:10:09 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=20=D0=B1=D0=B8=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=B2=20=D1=82=D0=B5=D0=BA=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab3/app/Main.hs | 5 ++++- lab3/src/Lib.hs | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lab3/app/Main.hs b/lab3/app/Main.hs index 70ebd09..e0a98cb 100644 --- a/lab3/app/Main.hs +++ b/lab3/app/Main.hs @@ -13,4 +13,7 @@ main = do putStrLn $ show (length alphabet) let encryptedText = encryptCaesar alphabet caesarShift inputText putStrLn $ take 30 encryptedText - putStrLn $ concat (take 30 (map show (textToBits encryptedText))) \ No newline at end of file + let encryptedTextBits = textToBits encryptedText + putStrLn $ concat (take 30 (map show encryptedTextBits)) + let encryptedTextFromBits = bitsToText encryptedTextBits + putStrLn $ take 30 encryptedTextFromBits \ No newline at end of file diff --git a/lab3/src/Lib.hs b/lab3/src/Lib.hs index dce7c5e..8b11de5 100644 --- a/lab3/src/Lib.hs +++ b/lab3/src/Lib.hs @@ -2,10 +2,11 @@ module Lib ( createAlphabetFromText, encryptCaesar, - textToBits + textToBits, + bitsToText ) where -import Data.Char (ord) +import Data.Char (ord, chr) import Data.Bits (testBit) createAlphabetFromText :: String -> [Char] @@ -30,3 +31,10 @@ encryptCaesar alphabet shift text = map caesarChar text textToBits :: String -> [Int] textToBits = concatMap charToBits where charToBits c = [ if testBit (ord c) i then 1 else 0 | i <- [7,6..0] ] + +bitsToText :: [Int] -> String +bitsToText [] = [] +bitsToText bits = (chr $ bitsToInt (take 8 bits)) : bitsToText (drop 8 bits) + where + bitsToInt charBits = + sum [bit * (2 ^ index) | (bit, index) <- zip charBits [7 :: Int, 6 .. 0]] \ No newline at end of file