简体   繁体   English

Haskell 将 Word8 转换为 Int 到 Bits

[英]Haskell convert Word8 to Int to Bits

I want to convert a Word8 to an Int so I can convert it to a list of Bits is there anyway to convert it to either an int or straight to bits?我想将 Word8 转换为 Int,以便我可以将其转换为 Bits 列表,无论如何将其转换为 int 或直接转换为位?

This is my current snippet of a code这是我当前的代码片段

intsToBits :: Int -> [Bit]
intsToBits x  =
  reverse (go x) where
  go 0 = [Zero]
  go n | n `mod` 2 == 1 = One : go (n `div` 2)
       | n `mod` 2 == 0 = Zero : go (n `div` 2)
  go _ = []

word8ToInt :: Word8 -> Int
word8ToInt = fromIntegral

decoder :: BS.ByteString -> IO [Bit]
decoder raw =
  do
     let raw' = BS.unpack raw
     let ls = go raw'
     return ls
       where
         go []     = []
         go [x]    = intsToBits $  x
         go (x:xs) = intsToBits (word8ToInt x) ++ go xs

Word8 is an instance of the FiniteBits class, so you can use something like Word8FiniteBits类的一个实例,因此您可以使用类似

toBitList x = map (testBit x) [0..(finiteBitSize x-1)]

Or if you want it the other way around, [(finiteBitSize x-1), (finiteBitSize x-2)..0] .或者,如果你想[(finiteBitSize x-1), (finiteBitSize x-2)..0][(finiteBitSize x-1), (finiteBitSize x-2)..0]


I see from your edit that you (apparently) want your bits in order from most significant to least significant, and you don't want any leading zeros.我从您的编辑中看到您(显然)希望您的位按从最重要到最不重要的顺序排列,并且您不想要任何前导零。 There are various ways to do this, but the easiest is probably to modify the above approach using countLeadingZeros .有多种方法可以做到这一点,但最简单的方法可能是使用countLeadingZeros修改上述方法。

Meta on asking questions: You really should give some examples of how you tried to solve the problem.关于提问的元:你真的应该举一些例子来说明你是如何尝试解决问题的。 Shy of that, at least an example of the result you want such as what "bits" means to you.害羞的是,至少有一个你想要的结果的例子,比如“位”对你意味着什么。 I tend to think of a collection of Word8 values as bits and so there is operation to be performed.我倾向于将Word8值的集合视为位,因此需要执行操作。

That said, lets pose and answer a series of related questions.也就是说,让我们提出并回答一系列相关问题。

What are the bit operators in Haskell? Haskell 中的位运算符是什么?

If you want bit level operations, instead of doing arithmetic tricks like x || 2^i如果你想要位级操作,而不是像x || 2^i这样的算术技巧x || 2^i you can use the functions from the Bits class: x || 2^i您可以使用 Bits 类中的函数:

Prelude> import Data.Bits
Prelude Data.Bits> :i Bits
class Eq a => Bits a where
  (.&.) :: a -> a -> a
  (.|.) :: a -> a -> a
  xor :: a -> a -> a
  complement :: a -> a
  shift :: a -> Int -> a
  rotate :: a -> Int -> a
  zeroBits :: a
  bit :: Int -> a
  setBit :: a -> Int -> a
  clearBit :: a -> Int -> a
  complementBit :: a -> Int -> a
  testBit :: a -> Int -> Bool
  bitSizeMaybe :: a -> Maybe Int
  bitSize :: a -> Int
  isSigned :: a -> Bool
  shiftL :: a -> Int -> a
  unsafeShiftL :: a -> Int -> a
  shiftR :: a -> Int -> a
  unsafeShiftR :: a -> Int -> a
  rotateL :: a -> Int -> a
  rotateR :: a -> Int -> a
  popCount :: a -> Int

And also the FiniteBits class:还有FiniteBits类:

class Bits b => FiniteBits b where
  finiteBitSize      :: b -> Int
  countLeadingZeros  :: b -> Int
  countTrailingZeros :: b -> Int

How can I convert Word8 to [Bool] ?如何将Word8转换为[Bool]

This is simply testing each bit in the word, and b这只是测试字中的每一位,而 b

toBits x = [testBit x i | i <- [0.. finiteBitSize x - 1]

How can I convert Word8 to some datatype I called Bit that I won't show you?如何将Word8转换为某种我称为Bit数据类型,但我不会向您展示?

After your first edit I now think you have data Bit = Zero | One在您第一次编辑后,我现在认为您有data Bit = Zero | One data Bit = Zero | One but am unclear if that's correct. data Bit = Zero | One但不清楚这是否正确。 It seems like homework and the advice given here should suffice to get the rest of the way either way.似乎家庭作业和这里给出的建议应该足以完成剩下的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM