gpt4 book ai didi

prolog - 在 SWI-Prolog 中,是否有一种使用 REPL 将数字从一个基数转换为另一个基数的简单方法?

转载 作者:行者123 更新时间:2023-12-01 21:59:19 25 4
gpt4 key购买 nike

在使用 REPL 的 SWI-Prolog 中,可以轻松地将任何基数转换为基数 10,例如

?- X = 16'FF.
X = 255.

?- X = 2'11111111.
X = 255.

但是这失败了。 (没想到它会起作用,但显示了我的想法。)

?- 2'X = 16'FF.
ERROR: Syntax error: Operator expected
ERROR:
ERROR: ** here **
ERROR: 2'X = 16'FF .

最佳答案

在 SWI-Prolog 中,您可以对 format/2 中的 radix 使用 r :

Print integer in radix numeric argument notation. Thus ~16r prints its argument hexadecimal. The argument should be in the range [2, ... , 36]. Lowercase letters are used for digits above 9. The colon modifier may be used to form locale-specific digit groups.

例子:

?- format("~2r", 0xFF).11111111true.

关于prolog - 在 SWI-Prolog 中,是否有一种使用 REPL 将数字从一个基数转换为另一个基数的简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54297285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com