Биты хранятся в Vector

This commit is contained in:
2024-11-18 01:35:40 +03:00
parent abb078e1b6
commit e6d636cff2
4 changed files with 16 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
module Main (main) where
import Codec.Picture
import qualified Data.Vector.Unboxed as VU
import Lib
caesarShift :: Int
@@ -15,7 +16,7 @@ main = do
let encryptedText = encryptCaesar alphabet caesarShift inputText
putStrLn $ take 30 encryptedText
let encryptedTextBits = textToBits encryptedText
putStrLn $ concat (take 30 (map show encryptedTextBits))
putStrLn $ concat (take 30 (map show (VU.toList encryptedTextBits)))
let encryptedTextFromBits = bitsToText encryptedTextBits
putStrLn $ take 30 encryptedTextFromBits
let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromBits

View File

@@ -36,6 +36,7 @@ library
build-depends:
JuicyPixels
, base >=4.7 && <5
, vector
default-language: Haskell2010
executable lab3-exe
@@ -51,6 +52,7 @@ executable lab3-exe
JuicyPixels
, base >=4.7 && <5
, lab3
, vector
default-language: Haskell2010
test-suite lab3-test
@@ -67,4 +69,5 @@ test-suite lab3-test
JuicyPixels
, base >=4.7 && <5
, lab3
, vector
default-language: Haskell2010

View File

@@ -22,6 +22,7 @@ description: Please see the README on GitHub at <https://github.com/gith
dependencies:
- base >= 4.7 && < 5
- JuicyPixels
- vector
ghc-options:
- -Wall

View File

@@ -9,6 +9,7 @@ module Lib
import Data.Char (ord, chr)
import Data.Bits (testBit)
import qualified Data.Vector.Unboxed as VU
createAlphabetFromText :: String -> [Char]
createAlphabetFromText [] = []
@@ -35,13 +36,15 @@ decryptCaesar alphabet shift =
where
alphabetLength = length alphabet
textToBits :: String -> [Int]
textToBits = concatMap charToBits
where charToBits c = [ if testBit (ord c) i then 1 else 0 | i <- [7,6..0] ]
textToBits :: String -> VU.Vector Int
textToBits text = VU.fromList $ concatMap charToBits text
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)
bitsToText :: VU.Vector Int -> String
bitsToText bits
| VU.null bits = []
| otherwise = (chr $ bitsToInt (VU.take 8 bits)) : bitsToText (VU.drop 8 bits)
where
bitsToInt charBits =
sum [bit * (2 ^ index) | (bit, index) <- zip charBits [7 :: Int, 6 .. 0]]
sum [bit * (2 ^ index) | (bit, index) <- zip (VU.toList charBits) [7 :: Int,6..0]]