Data.ByteString.Lazy.Internal.ByteString to string?

前端 未结 2 1429
猫巷女王i
猫巷女王i 2021-02-13 14:14

Trying to write a module which returns the external IP address of my computer. Using Network.Wreq get function, then applying a lense to obtain r

2条回答
  •  日久生厌
    2021-02-13 14:32

    Short answer: Use unpack from Data.ByteString.Lazy.Char8

    Longer answer:

    In general when you want to convert a ByteString (of any variety) to a String or Text you have to specify an encoding - e.g. UTF-8 or Latin1, etc.

    When retrieving an HTML page the encoding you are suppose to use may appear in the Content-type header or in the response body itself as a tag.

    Alternatively you can just guess at what the encoding of the body is.

    In your case I presume you are accessing a site like http://whatsmyip.org and you only need to parse out your IP address. So without examining the headers or looking through the HTML, a safe encoding to use would be Latin1.

    To convert ByteStrings to Text via an encoding, have a look at the functions in Data.Text.Encoding

    For instance, the decodeLatin1 function.

提交回复
热议问题