Skip to content

Instantly share code, notes, and snippets.

@dminuoso

dminuoso/f.hs Secret

Created March 2, 2023 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminuoso/8c4eb6a1d996f4e226f9ecce23c35895 to your computer and use it in GitHub Desktop.
Save dminuoso/8c4eb6a1d996f4e226f9ecce23c35895 to your computer and use it in GitHub Desktop.
data IPv4 = IPv4 Word32
deriving (Eq, Ord, Show)
ipv4ToWord8s :: IPv4 -> (Word8, Word8, Word8, Word8)
ipv4ToWord8s (IPv4 addr) = ( fromIntegral (addr `unsafeShiftR` 24)
, fromIntegral (addr `unsafeShiftR` 16)
, fromIntegral (addr `unsafeShiftR` 8)
, fromIntegral addr
)
ipv4FromWord8s :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4
ipv4FromWord8s a b c d = IPv4 $ (fromIntegral a `shiftL` 24)
.|. (fromIntegral b `shiftL` 16)
.|. (fromIntegral c `shiftL` 8)
.|. (fromIntegral d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment