reworked encoding for binary and octal in way that ASCII is encoded to binary or octal Integral

This commit is contained in:
Stefan Friese 2024-05-14 00:18:48 +02:00
parent fd6b5dfd16
commit a5c73c27f6
1 changed files with 39 additions and 8 deletions

47
Main.hs
View File

@ -4,12 +4,14 @@
module Main where module Main where
import System.Console.CmdArgs import System.Console.CmdArgs
import Control.Arrow import Control.Arrow
import Data.Text (pack, unpack) import Data.Text (pack, unpack, Text)
import Data.Bits
-- import Data.Char
import Text.Read (readMaybe) import Text.Read (readMaybe)
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Encoding as T import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T import qualified Data.Text.IO as T
import Data.Text (Text) -- import Data.Text (Text)
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
import Data.Text.Lazy.Builder (toLazyText) import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Encoding (decodeUtf8With, decodeUtf8, encodeUtf8) import Data.Text.Encoding (decodeUtf8With, decodeUtf8, encodeUtf8)
@ -42,12 +44,11 @@ import Data.Bytes.Get (getWord64host)
import Data.ByteString.UTF8 as BSU -- from utf8-string import Data.ByteString.UTF8 as BSU -- from utf8-string
-- import Data.ByteString (singleton) -- import Data.ByteString (singleton)
import GHC.Word (Word8) import GHC.Word (Word8)
-- import Data.Char
import qualified Data.ByteString.Char8 as C import qualified Data.ByteString.Char8 as C
-- Regex imports -- Regex imports
import Text.Regex.TDFA import Text.Regex.TDFA
import Data.Word (Word8) import Data.Word (Word8)
import Data.Char (ord, chr) import Data.Char (ord, chr, intToDigit)
data Based = Decode { data Based = Decode {
b91 :: Bool, b91 :: Bool,
@ -239,7 +240,7 @@ dec58 input =
maybe "Error decoding Base58.\n" C.unpack (B58.decodeBase58 (T.pack input)) maybe "Error decoding Base58.\n" C.unpack (B58.decodeBase58 (T.pack input))
enc58 :: String -> String enc58 :: String -> String
enc58 = unpack . B58.encodeBase58 . BSU.fromString enc58 = T.unpack . B58.encodeBase58 . BSU.fromString
dec32 :: String -> String dec32 :: String -> String
-- dec32 = C.unpack . U.fromRight . B32.decode . BSU.fromString -- dec32 = C.unpack . U.fromRight . B32.decode . BSU.fromString
@ -265,17 +266,47 @@ dec10 :: String -> String
dec10 = show . hexStringToInt dec10 = show . hexStringToInt
enc10 :: String -> String enc10 :: String -> String
enc10 = C.unpack . BSU.fromString . intToHexString . decimalStringToInt -- Depending on what you want, do enc10 = show . map ord -- enc10 = C.unpack . BSU.fromString . intToHexString . decimalStringToInt -- Depending on what you want, do enc10 = show . map ord
enc10 str = C.unpack $ C.pack $ Prelude.foldl (\acc char -> acc ++ show (ord char)) "" str
dec8 :: String -> String
dec8 = C.unpack . encodeUtf8 . toText . showbOct . hexStringToInt dec8 = C.unpack . encodeUtf8 . toText . showbOct . hexStringToInt
enc8 = C.unpack . BSU.fromString . intToHexString . octToInt . (reverse . (map fromJust . (map fromOctDigit)))
-- enc8 :: String -> String
-- enc8 = C.unpack . BSU.fromString . intToHexString . octToInt . (reverse . (map fromJust . (map fromOctDigit)))
--
unicodeToOctal :: Char -> String
unicodeToOctal c = reverse $ padTo3Bits $ decimalToOctal' (ord c)
where
decimalToOctal' 0 = "0"
decimalToOctal' m = let (q, r) = m `divMod` 8 in intToDigit r : decimalToOctal' q
padTo3Bits :: String -> String
padTo3Bits bits = replicate (3 - Prelude.length bits) '0' ++ bits
enc8 :: String -> String
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 dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt
-- dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt . enc16
enc2 :: String -> String 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 = concatMap (\c -> padTo7Bits (decimalToBinary (ord c))) input
where
decimalToBinary :: Int -> String
decimalToBinary 0 = "0"
decimalToBinary n = reverse $ decimalToBinary' n
where
decimalToBinary' 0 = ""
decimalToBinary' m = let (q, r) = m `divMod` 2 in (if r == 0 then '0' else '1') : decimalToBinary' q
padTo7Bits :: String -> String
padTo7Bits bits = replicate (7 - Prelude.length bits) '0' ++ bits
decqp :: String -> String decqp :: String -> String
-- decqp = C.unpack . U.fromRight . QP.decode . BSU.fromString -- decqp = C.unpack . U.fromRight . QP.decode . BSU.fromString