0

I'm using the Data.Binary.Get monad to read data from a file in Haskell. How would I go about converting some Word32 grabbed from getWord32** into a [Char] or String?

I've tried breaking the Word32 into four octets and converting that way, but I'm having trouble getting the conversion working. How would I best convert a Word32 into a string (assuming four characters).

1
  • Does show work ? You can see how it's instance is implemented in base. Commented Oct 11, 2015 at 0:23

1 Answer 1

1

The package utf8-light has a function named w2c function whose type is:

w2c ::Word32 -> Char

You can see how it's implemented:

w2c :: Word32 -> Char
{-# INLINE w2c #-}
#if defined(__GLASGOW_HASKELL__)
w2c (W32# w) = C#(chr#(word2Int# w))
#else
w2c = unsafeChr . fromIntegral
#endif

You can write a wrapper around it:

w2s :: Word32 -> String
w2s xs = [xs]

Alternatively, you can also use the show function since Word32 has an instance for it. But I guess, it has a performance penalty.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.