Convert integer to hexadecimal

Discussions related to Visual Prolog
Loffy
Active Member
Posts: 30
Joined: 15 Aug 2019 11:32

Convert integer to hexadecimal

Unread post by Loffy » 26 Aug 2019 6:45

I wish to convert an integer number to hexadecimal. For example 255 would become a single byte FF.

Any hints?

Regards,

Loffy

B.Hooijenga
VIP Member
Posts: 100
Joined: 11 Jul 2002 23:01

Re: Convert integer to hexadecimal

Unread post by B.Hooijenga » 26 Aug 2019 11:44

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]

Harrison Pratt
VIP Member
Posts: 303
Joined: 5 Nov 2000 0:01

Re: Convert integer to hexadecimal

Unread post by Harrison Pratt » 26 Aug 2019 11:56

string::format

Code: Select all

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

Loffy
Active Member
Posts: 30
Joined: 15 Aug 2019 11:32

Re: Convert integer to hexadecimal

Unread post by Loffy » 26 Aug 2019 12:09

Thank you both.

I can see reasons to use both suggestions.

Loffy

Loffy
Active Member
Posts: 30
Joined: 15 Aug 2019 11:32

Re: Convert integer to hexadecimal

Unread post by Loffy » 26 Aug 2019 12:31

Tested and working.

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

Regards,

Loffy

Post Reply