Ami
Ami

Reputation: 363

Prolog: How to convert hexadecimal value to integer in Prolog

Prolog: How to convert hexadecimal value to integer in Prolog

My input value is 0x10 and I want to increment it in Prolog.

increment(_incremented_value) :-
  _my_var= 0x10,
  _incremented_value is _my_var + 1.

but this is giving error, saying _my_var is not integer

Upvotes: 1

Views: 863

Answers (1)

User9213
User9213

Reputation: 1316

You don't convert anything, Prolog understand hexadecimal literals. By default it prints integers in base 10, so you get:

?- X = 0x10.
X = 16.

?- X = 0xAB.
X = 171.

You can use formatted printing to print the integer in any base you like. In SWI-Prolog:

?- X = 0xFF, format("~2r", [X]).
11111111
X = 255.

?- X = 0xFF, format("~36r", [X]).
73
X = 255.

?- X = 3, format("~3r", [X]).
10
X = 3.

If you are asking something else you should make it clear what it is.

Upvotes: 1

Related Questions