Last active
April 3, 2021 11:06
-
-
Save jeshan/bfce96ac13274be2ae40cf53eb4f47d9 to your computer and use it in GitHub Desktop.
Bi-directional conversion between hex strings and numbers in Prolog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- use_module(library(clpfd)). | |
hex_number(Hex, Num) :- | |
ground(Hex), | |
!, | |
string_concat("0x", Hex, HexStr), | |
number_string(Num, HexStr). | |
hex_number(Hex, Num) :- | |
var(Hex), | |
!, | |
Num in 0..0xfffffff, | |
label([Num]), | |
format(string(Hex), "~16r", Num). |
Code is released in the public domain.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example outputs: