Из битов в текст
This commit is contained in:
@@ -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)))
|
||||
let encryptedTextBits = textToBits encryptedText
|
||||
putStrLn $ concat (take 30 (map show encryptedTextBits))
|
||||
let encryptedTextFromBits = bitsToText encryptedTextBits
|
||||
putStrLn $ take 30 encryptedTextFromBits
|
||||
@@ -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]]
|
||||
Reference in New Issue
Block a user