如何在haskell中以二进制或十六进制打印整数文字?
如何在haskell中以二进制或十六进制打印整数文字?
printBinary 5 => "0101"
printHex 5 => "05"
哪些库/函数允许这样做?
我遇到了 Numeric 模块及其 showIntAtBase 函数,但无法正确使用它。
> :t showIntAtBase
showIntAtBase :: (Integral a) => a -> (Int -> Char) -> a -> String -> String
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您还可以使用 printf 包的 printf 使用 c 样式格式描述符来格式化输出:
输出:
You may also use printf of the printf package to format your output with c style format descriptors:
Output:
如果导入
Numeric
和Data.Char
模块,您可以执行以下操作:这适用于 16 以内的任何基数,因为这就是
intToDigit< /code> 适用于。上面示例中额外的空字符串参数的原因是
showIntAtBase
返回一个ShowS
类型的函数,该函数会将显示表示形式连接到现有字符串上。If you import the
Numeric
andData.Char
modules, you can do this:This will work for any bases up to 16, since this is all that
intToDigit
works for. The reason for the extra empty string argument in the examples above is thatshowIntAtBase
returns a function of typeShowS
, which will concatenate the display representation onto an existing string.您可以使用如下所示将整数转换为二进制:
GHCi 中的用法:
You can convert integer to binary with something like the following:
usage in GHCi:
十六进制可以用
0x
编写,二进制可以用0b
前缀编写,例如:请注意,二进制需要
BinaryLiterals
扩展名。Hex can be written with
0x
and binary with0b
prefix e.g.:Note that binary requires the
BinaryLiterals
extension.您可以定义自己的递归函数,例如:
说明:
如您所见,toNum 函数使用给定的基数和映射函数将基于 k 的值转换为十进制。映射函数会将特殊字符映射到十进制值(例如,A=10、B=11、...,十六进制)。对于二进制映射,您还可以使用 lambda 表达式,如 binToDec 中所示。
而 toKBaseVal 函数则相反,将小数转换为基于 k 的值。同样,我们需要一个执行相反操作的映射函数:从小数到基于 k 的值的相应特殊字符。
作为测试,您可以输入:
假设您想要从十进制转换为八进制:
同样,我只使用 lambda 表达式,因为映射很简单:只需将 int 转换为 digital。
希望有帮助!很好的编程!
You could define your own recursive functions like:
Explanation:
As you can see, the toNum function converts a k-based value to decimal, using the given base and a mapping function. The mapping function will map special characters to a decimal value (for ex. A=10, B=11, ... in hex). For binary mapping you could also use a lambda expression like you see in binToDec.
Whereas the toKBaseVal function is the opposite, converting a decimal to a k-based value. Again we need a mapping function which does the opposite: from a decimal to the corresponding special character of the k-based value.
As a test you can type:
Suppose you want to convert from decimal to octal:
Again, I use just a lambda expression, because the mapping is simple: just int to digit.
Hope that helps! Good programming!
对于单行爱好者来说愚蠢的解决方案:
单行的核心是:
为了清楚起见,您也可以将以下内容放入文件中:
并使用以下命令执行脚本:
我使用了定点运算符,因此它是一个定点运算符的示例;还因为它允许我严格自下而上地构建单行代码。 (注意:不鼓励自下而上的开发。)
参考:堆栈脚本语法,命令行参数,
修复
运算符定义。递归 haskell 十六进制 haskell-stack
Silly solution for one-liner fans:
The nucleus of the one-liner is:
For the sake of clarity, you can, alternatively, put the following in a file:
And execute the script with:
I used fixed-point operator just so it is an example with fixed-point operator; also because it allowed me to construct the one-liner strictly bottom-up. (Note: bottom-up development is to be discouraged.)
References: stack script syntax, Command line arguments,
fix
operator definition.recursion haskell hex haskell-stack
这是一个简单、高效、与基础无关的无许可实现:
您必须
导入 Data.Word
使用Word8
(它尽可能合理地限制值),并且您经常需要fromIntegral
(如果只有自动类型转换的话.. .)。Here is a simple, efficient, base-agnostic, Unlicenced implementation:
You have to
import Data.Word
to useWord8
(which limits the values as much as reasonably possible), and you will often needfromIntegral
(if only automatic type conversions were a thing...).使用
FiniteBits
类:示例:
showBits (4 :: Word8)
=>“00000100”
showBits (50::Int16)
=>"0000000000110010"
showBits (-127::Int32)
=>“11111111111111111111111110000001”
Using the
FiniteBits
class:Examples:
showBits (4 :: Word8)
=>"00000100"
showBits (50 :: Int16)
=>"0000000000110010"
showBits (-127 :: Int32)
=>"11111111111111111111111110000001"
使用
text
时,我建议使用text-show
包,其中包括:showbBin :: (Integral a, TextShow a )=>一个->生成器
showbHex :: (Integral a, TextShow a) =>;一个->生成器
showbOct :: (Integral a, TextShow a) =>;一个->生成器
showbIntAtBase :: (Integral a, TextShow a) =>一个-> (Int -> Char) -> 字符一个-> Builder
例如,将
Integer
转换为二进制的Text
:也许您想添加
Text
前缀。Builder
允许您高效地构造Text
;它是一个幺半群。有关详细信息,请参阅
TextShow
和TextShow.Data.Integral
模块。When working with
text
, I recommend using thetext-show
package which includes:showbBin :: (Integral a, TextShow a) => a -> Builder
showbHex :: (Integral a, TextShow a) => a -> Builder
showbOct :: (Integral a, TextShow a) => a -> Builder
showbIntAtBase :: (Integral a, TextShow a) => a -> (Int -> Char) -> a -> Builder
For example, converting an
Integer
toText
in binary:Perhaps you want to add a
Text
prefix.Builder
allows you to efficiently constructText
; it is a monoid.For more information see the
TextShow
andTextShow.Data.Integral
modules available on Hackage.Numeric 模块包含多个用于显示整数类型的函数在各种基础上,包括 showIntAtBase。以下是一些使用示例:
The Numeric module includes several functions for showing an Integral type at various bases, including
showIntAtBase
. Here are some examples of use: