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

Convert integer to hexadecimal

Unread post by Loffy »

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

Any hints?

Regards,

Loffy
B.Hooijenga
Active Member
Posts: 47
Joined: 11 Jul 2002 23:01

Re: Convert integer to hexadecimal

Unread post 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]
Harrison Pratt
VIP Member
Posts: 338
Joined: 5 Nov 2000 0:01

Re: Convert integer to hexadecimal

Unread post 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"                
Loffy
Active Member
Posts: 47
Joined: 15 Aug 2019 11:32

Re: Convert integer to hexadecimal

Unread post by Loffy »

Thank you both.

I can see reasons to use both suggestions.

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

Re: Convert integer to hexadecimal

Unread post 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
Post Reply