如何在haskell中以二进制或十六进制打印整数文字? [英] How to print integer literals in binary or hex in haskell?
本文介绍了如何在haskell中以二进制或十六进制打印整数文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
printBinary 5 =>如何在haskell中以二进制或十六进制打印整数文字? 0101
printHex 5 => 05
哪些库/函数允许这样做?
我遇到了Numeric模块及其showIntAtBase函数,但一直无法正确使用它。
> :t showIntAtBase
showIntAtBase ::(Integral a)=> a - > (Int→Char)→> a - >字符串 - >字符串
解决方案
数字模块包含几个在不同基地显示Integral类型的函数,包括 showIntAtBase
。这里有一些使用的例子:
$ $ p $ import数字(showHex,showIntAtBase)
导入Data.Char(intToDigit)
putStrLn $ showHex 12 - 打印c
putStrLn $ showIntAtBase 2 intToDigit 12 - 打印1100
How to print integer literals in binary or hex in haskell?
printBinary 5 => "0101"
printHex 5 => "05"
Which libraries/functions allow this?
I came across the Numeric module and its showIntAtBase function but have been unable to use it correctly.
> :t showIntAtBase
showIntAtBase :: (Integral a) => a -> (Int -> Char) -> a -> String -> String
解决方案
The Numeric module includes several functions for showing an Integral type at various bases, including showIntAtBase
. Here are some examples of use:
import Numeric (showHex, showIntAtBase)
import Data.Char (intToDigit)
putStrLn $ showHex 12 "" -- prints "c"
putStrLn $ showIntAtBase 2 intToDigit 12 "" -- prints "1100"
这篇关于如何在haskell中以二进制或十六进制打印整数文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文