reworked b2 and b8 functions
This commit is contained in:
parent
dec7ccc3c5
commit
0b5f07477e
59
Main.hs
59
Main.hs
|
@ -270,15 +270,18 @@ enc10 :: String -> String
|
||||||
enc10 str = C.unpack $ C.pack $ Prelude.foldl (\acc char -> acc ++ show (ord char)) "" str
|
enc10 str = C.unpack $ C.pack $ Prelude.foldl (\acc char -> acc ++ show (ord char)) "" str
|
||||||
|
|
||||||
-- decode octal
|
-- decode octal
|
||||||
|
-- octalToChar :: String -> Char
|
||||||
|
-- octalToChar str = chr $ Prelude.foldl (\acc c -> acc * 8 + read [c]) 0 str
|
||||||
|
|
||||||
|
-- Function to decode a single octal value to its corresponding character
|
||||||
octalToChar :: String -> Char
|
octalToChar :: String -> Char
|
||||||
octalToChar str = chr $ Prelude.foldl (\acc c -> acc * 8 + read [c]) 0 str
|
octalToChar octal = chr (read ("0o" ++ octal)) -- Assumes input is in base 8 (octal)
|
||||||
|
|
||||||
chunksOf :: Int -> [a] -> [[a]]
|
-- Function to split a string into chunks of three characters each
|
||||||
chunksOf _ [] = []
|
chunksOf3 :: String -> [String]
|
||||||
chunksOf n xs = Prelude.take n xs : chunksOf n (Prelude.drop n xs)
|
chunksOf3 [] = []
|
||||||
|
chunksOf3 str = Prelude.take 3 str : chunksOf3 (Prelude.drop 3 str)
|
||||||
|
|
||||||
decodeOctal :: String -> String
|
|
||||||
decodeOctal = map octalToChar . words
|
|
||||||
|
|
||||||
-- decodeOctal :: String -> String
|
-- decodeOctal :: String -> String
|
||||||
-- decodeOctal = map (octalToChar . padOctal) . words
|
-- decodeOctal = map (octalToChar . padOctal) . words
|
||||||
|
@ -291,7 +294,7 @@ decodeOctal = map octalToChar . words
|
||||||
|
|
||||||
-- Function to decode a string of octal numbers to characters
|
-- Function to decode a string of octal numbers to characters
|
||||||
dec8 :: String -> String
|
dec8 :: String -> String
|
||||||
dec8 = map octalToChar . chunksOf 3 . filter (/= ' ')
|
dec8 = map octalToChar . words
|
||||||
|
|
||||||
-- dec8 :: String -> String
|
-- dec8 :: String -> String
|
||||||
-- dec8 = C.unpack . encodeUtf8 . toText . showbOct . hexStringToInt
|
-- dec8 = C.unpack . encodeUtf8 . toText . showbOct . hexStringToInt
|
||||||
|
@ -330,6 +333,11 @@ dec8 = map octalToChar . chunksOf 3 . filter (/= ' ')
|
||||||
-- enc8 :: String -> String
|
-- enc8 :: String -> String
|
||||||
-- enc8 = unwords . map (concatMap unicodeToOctal . (:[]))
|
-- enc8 = unwords . map (concatMap unicodeToOctal . (:[]))
|
||||||
|
|
||||||
|
chunksOf :: Int -> [a] -> [[a]]
|
||||||
|
chunksOf _ [] = []
|
||||||
|
chunksOf n xs = Prelude.take n xs : chunksOf n (Prelude.drop n xs)
|
||||||
|
|
||||||
|
|
||||||
unicodeToOctal :: Char -> [String]
|
unicodeToOctal :: Char -> [String]
|
||||||
unicodeToOctal c = chunksOf 3 $ reverse $ decimalToOctal' (ord c)
|
unicodeToOctal c = chunksOf 3 $ reverse $ decimalToOctal' (ord c)
|
||||||
where
|
where
|
||||||
|
@ -351,25 +359,36 @@ enc8 = unwords . concatMap unicodeToOctal
|
||||||
-- enc8 :: String -> String
|
-- enc8 :: String -> String
|
||||||
-- enc8 = concatMap unicodeToOctal
|
-- enc8 = concatMap unicodeToOctal
|
||||||
|
|
||||||
dec2 :: String -> String
|
-- dec2 :: String -> String
|
||||||
-- dec2 = C.unpack . encodeUtf8 . toText . showbBin . hexStringToInt
|
-- dec2 = C.unpack . encodeUtf8 . toText . showbBin . hexStringToInt
|
||||||
-- dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt . enc16
|
-- dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt . enc16
|
||||||
dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt
|
-- dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt
|
||||||
|
--
|
||||||
|
binaryToChar :: String -> Char
|
||||||
|
binaryToChar binStr = chr $ binaryToInt binStr
|
||||||
|
|
||||||
|
binaryToInt :: String -> Int
|
||||||
|
binaryToInt binStr = Prelude.foldl (\acc x -> acc * 2 + digitToInt x) 0 binStr
|
||||||
|
|
||||||
|
dec2 :: String -> String
|
||||||
|
dec2 input = map binaryToChar $ words input
|
||||||
|
|
||||||
|
|
||||||
enc2 :: String -> String
|
|
||||||
-- enc2 = C.unpack . BSU.fromString . intToHexString . binToInt . (reverse . (map fromJust . (map fromBinDigit)))
|
-- enc2 = C.unpack . BSU.fromString . intToHexString . binToInt . (reverse . (map fromJust . (map fromBinDigit)))
|
||||||
-- enc2 input = C.unpack $ C.pack $ show $ Prelude.foldl (\acc char -> (acc `shiftL` 8) .|. fromIntegral (ord char)) BSU.fromString
|
-- enc2 input = C.unpack $ C.pack $ show $ Prelude.foldl (\acc char -> (acc `shiftL` 8) .|. fromIntegral (ord char)) BSU.fromString
|
||||||
enc2 input = concatMap (\c -> padTo7Bits (decimalToBinary (ord c))) input
|
charToBinary :: Char -> String
|
||||||
where
|
charToBinary char = let binaryStr = intToBinary $ ord char
|
||||||
decimalToBinary :: Int -> String
|
in replicate (7 - Prelude.length binaryStr) '0' ++ binaryStr
|
||||||
decimalToBinary 0 = "0"
|
|
||||||
decimalToBinary n = reverse $ decimalToBinary' n
|
intToBinary :: Int -> String
|
||||||
where
|
intToBinary n = reverse $ decimalToBinary' n
|
||||||
decimalToBinary' 0 = ""
|
where
|
||||||
decimalToBinary' m = let (q, r) = m `divMod` 2 in (if r == 0 then '0' else '1') : decimalToBinary' q
|
decimalToBinary' 0 = "0"
|
||||||
padTo7Bits :: String -> String
|
decimalToBinary' m = let (q, r) = m `divMod` 2 in intToDigit r : decimalToBinary' q
|
||||||
padTo7Bits bits = replicate (7 - Prelude.length bits) '0' ++ bits
|
|
||||||
|
-- enc2 :: String -> String
|
||||||
|
enc2 :: String -> String
|
||||||
|
enc2 input = unwords $ map charToBinary input
|
||||||
|
|
||||||
|
|
||||||
decqp :: String -> String
|
decqp :: String -> String
|
||||||
|
|
Loading…
Reference in New Issue