Currently I can convert a decimal number to a hexadecimal number, pad it on the left with zeros and upper case the hexadecimal digits A - F
using
?- Value = 13,format(atom(Atom_lower),'~|~`0t~16r~2|',[Value]),upcase_atom(Atom_lower,Atom).
Value = 13,
Atom_lower = '0d',
Atom = '0D'.
However the width is a constant, i.e. 2 and I don’t know if format/3 can take the width as an argument, something like
?- Value = 13,Width=2,format(atom(Atom_lower),'~|~`0t~16r???|',[Value,Width]),upcase_atom(Atom_lower,Atom).
Value = 13,
Atom_lower = '0d',
Atom = '0D'.
Is there something that ???
can be set to make this work? Is there a better way to do the conversion using other predicate(s)?
Right now I have
dec_to_hex(Value,Atom) :-
Value =< 0xFF, !,
format(atom(Atom_lower),'~|~`0t~16r~2|',Value),
upcase_atom(Atom_lower,Atom).
dec_to_hex(Value,Atom) :-
Value =< 0xFFFF, !,
format(atom(Atom_lower),'~|~`0t~16r~4|',Value),
upcase_atom(Atom_lower,Atom).
dec_to_hex(Value,Atom) :-
Value =< 0xFFFFFFFF, !,
format(atom(Atom_lower),'~|~`0t~16r~8|',Value),
upcase_atom(Atom_lower,Atom).
dec_to_hex(Value,Atom) :-
Value =< 0xFFFFFFFFFFFFFFFF,
format(atom(Atom_lower),'~|~`0t~16r~16|',Value),
upcase_atom(Atom_lower,Atom).
dec_to_hex(Value,Atom) :-
format(atom(Atom_lower),'~16r',Value),
upcase_atom(Atom_lower,Atom).
Examples
?- rfc5234:dec_to_hex(0,Atom).
Atom = '00'.
?- rfc5234:dec_to_hex(0xFF,Atom).
Atom = 'FF'.
?- rfc5234:dec_to_hex(0x100,Atom).
Atom = '0100'.
?- rfc5234:dec_to_hex(0xFFFF,Atom).
Atom = 'FFFF'.
?- rfc5234:dec_to_hex(0x10000,Atom).
Atom = '00010000'.
?- rfc5234:dec_to_hex(0xFFFFFFFFFFFFFFFFFF,Atom).
Atom = 'FFFFFFFFFFFFFFFFFF'.
Based on this old answer by Holger Kanwischer