title: What is the best way to convert a ByteString to an Int?
body: <p>I always run into the following error when trying to read a ByteString:<br>
  <code>Prelude.read: no parse</code>  </p>
  
  <p>Here's a sample of code that will cause this error to occur upon rendering in a browser:</p>
  
  <pre><code>factSplice :: SnapletSplice App App
  factSplice = do
      mbstr &lt;- getParam "input" -- returns user input as bytestring
      let str = maybe (error "splice") show mbstr
      let n = read str :: Int
      return [X.TextNode $ T.pack $ show $ product [1..n]]
  </code></pre>
  
  <p>Or perhaps more simply: </p>
  
  <pre><code>simple bs = read (show bs) :: Int
  </code></pre>
  
  <p>For some reason, after <code>show bs</code> the resulting string includes quotes.
  So in order to get around the error I have to remove the quotes then <code>read</code> it.
  I use the following function copied from the internet to do so:</p>
  
  <pre><code>sq :: String -&gt; String
  sq s@[c]                     = s
  sq ('"':s)  | last s == '"'  = init s
              | otherwise      = s
  sq ('\'':s) | last s == '\'' = init s
              | otherwise      = s
  sq s                         = s
  </code></pre>
  
  <p>Then <code>simple bs = read (sq.show bs) :: Int</code> works as expected.</p>
  
  <ol>
  <li>Why is this the case?</li>
  <li>What is the best way to convert a ByteString to an Int?</li>
  </ol>