Say I have the UTF8 string "Hello Snowman ☃!"
. It has 16 characters and takes up 18 bytes. How can I get haskell to show me the number of bytes this string takes up?
I've tried using Data.ByteArray, Data.Text, ByteString and in each case I have come up short.
You can use the excellent utf8-string package for this.
import qualified Data.ByteString as BS
import qualified Data.ByteString.UTF8 as UTF8
numBytesUtf8 :: String -> Int
numBytesUtf8 = BS.length . UTF8.fromString
Then, to use your example,
ghci> numBytesUtf8 "Hello Snowman ☃!"
18
Of course, you should probably not be doing this in the first place. UTF8.fromString
and BS.length
are probably the functions you want to use, but your strings probably ought to be already bytestrings for you to be interested in how many bytes it takes to encode them as such.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With