Converting character codes of hexadecimal digits to number

While number_codes/2 will convert the list of characters codes made up of the digits 0 to 9 it does not work work with hexadecimal digits 0 to F.

Found this little gem by Richard A. O’Keefe

number_hexcodes(Number,Hex_codes) :-
    atom_to_term([0'0,0'x|Hex_codes],Number,_).

For the reverse, decimal number to hexadecimal codes.

format((codes(Hex_codes)"~16r",Number).

I didn’t know atoms could be represented as a list of character codes.

Most SWI-Prolog predicates that require text at some argument accept all string representations (atom, string, list of characters/codes). The predicate name is either historical or ISO dictated. If the argument acts as an output, text is represented using the indicated type.

2 Likes