【问题标题】:GUI for BASE64 Encoder in Haskell using Gtk2hs and Glade使用 Gtk2hs 和 Glade 的 Haskell 中 BASE64 编码器的 GUI
【发布时间】:2014-06-20 02:38:10
【问题描述】:

我有以下问题。我尝试使用 Gtk2Hs 和 Glade 在 Haskell 中为 BASE64 编码器创建一个简单的 GUI。这是 Haskell 中的 BASE64 编码器示例。

{-# LANGUAGE OverloadedStrings #-}

import Data.ByteString.Base64
import Data.ByteString.Char8

main = do
    print $ unpack $ encode "Hello, world!"
    print $ decode "SGVsbG8sIHdvcmxkIQ=="

现在我想为此示例创建 GUI,但我希望能够输入任何值进行编码。我已经创建了包含以下组件的模板: - entry1(输入要编码的值) - 按钮(开始生成) - entry2(查看生成的值)

我的haskell代码:

entry1 <- builderGetObject hello castToEntry "entry1"
entry2 <- builderGetObject hello castToEntry "entry2"
button <- builderGetObject hello castToButton "button"
onClicked button $ do
    name2 <- get entry1 entryText
    set entry2 [ entryText := unpack $ encode name2]

编译时出现以下错误

Couldn't match expected type `ByteString' with actual type `String'
In the first argument of `encode', namely `name2'
In the second argument of `($)', namely `encode name2'
In the second argument of `(:=)', namely `unpack $ encode name2'

【问题讨论】:

    标签: haskell gtk base64 glade gtk2hs


    【解决方案1】:

    name2String 类型,而 encode 需要 ByteString。最简单的做法是使用Data.ByteString.Char8 中的pack 函数进行转换。 然而,这有一个问题:它只适用于 ASCII 代码点。如果用户输入非 ASCII 字符(לדוגמה、כזה)会发生什么?

    相反,我建议您对文本进行 UTF8 编码。为此,我会使用 text 包,它看起来像:

    import qualified Data.Text as T
    import qualified Data.Text.Encoding as TE
    
    encode $ TE.encodeUtf8 $ T.pack name2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 2011-05-18
      • 2015-07-14
      相关资源
      最近更新 更多