Page 1 of 1

Convert integer to hexadecimal

Posted: 26 Aug 2019 6:45
by Loffy
I wish to convert an integer number to hexadecimal. For example 255 would become a single byte FF.

Any hints?

Regards,

Loffy

Re: Convert integer to hexadecimal

Posted: 26 Aug 2019 11:44
by B.Hooijenga
Hello Lofty

Here are two possibilities.
Use the helpfile for information about class binary.

Code: Select all

predicates     testing : (). clauses     testing() :-         % For manipulating bytes (in hexadecimal form) you need class binary         Binary1 = binary::createAtomic(1),         binary::setIndexed_unsigned8(Binary1, 0, 255),         stdio::write(Binary1, "\n"),         %                 % A second possibility         Binary2 = binary::createAtomicFromIntegerList([255, 254]),         stdio::write(Binary2).
You get this output:

$[FF]
$[FF,00,00,00,FE,00,00,00]

Re: Convert integer to hexadecimal

Posted: 26 Aug 2019 11:56
by Harrison Pratt
string::format

Code: Select all

% Try this:         R = 12,         S = string::format("%1x", R),         stdio::write("\n", R, "\t", S), % S = "C"                

Re: Convert integer to hexadecimal

Posted: 26 Aug 2019 12:09
by Loffy
Thank you both.

I can see reasons to use both suggestions.

Loffy

Re: Convert integer to hexadecimal

Posted: 26 Aug 2019 12:31
by Loffy
Tested and working.

Just as a thought, and possibly not an elegant solution, would creating a custom symbol table domain have worked?

Regards,

Loffy