Discussion:
converting numbers
Nandini K
2003-06-06 02:10:15 UTC
Permalink
Hi Group,

Can u give me some idea on defining a procedure read_hex_num(X) that reads
hexadecimal numbers and converts them to decimal numbers.

For example:
?- read_hex_num(What).
20
What=32
Yes
?- read_hex_num(What).
10fe
What=4350
Yes


Thanks,

nan

_________________________________________________________________
They're big & powerful. The macho mean machines!
http://server1.msn.co.in/features/suv/index.asp SUVs are here to stay!


----------------
* To UNSUBSCRIBE, please use the HTML form at

http://www.swi-prolog.org/mailinglist.html

or send mail to prolog-***@swi.psy.uva.nl using the Subject: "unsubscribe"
(without the quotes) and *no* message body.

** An ARCHIVE of this list is maintained at

http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/
Mathias Fonkam
2003-06-06 20:56:34 UTC
Permalink
Not sure if last response I had on this got posted as
I didn't receive it myself, so this is again:

I think it's best to separate the reading of your hex
numbers (since there are many input sources we can
think of and many ways ot doing this) from the actual
task of converting between hex and decimal. The task
of converting from hex to decimal will be best viewed
as a relation, something like:
hex2dec(Hex,Dec)
which should be true if Hex is the Hexadecimal
representation of the decimal number given by Dec and
vice versa. I quick and dirty solution I can put
together is as follows (you may have to tidy this up a
little - let me know):

==============
hex2dec(0,0).
hex2dec(1,1).
hex2dec(2,2).
hex2dec(3,3).
hex2dec(4,4).
hex2dec(5,5).
hex2dec(6,6).
hex2dec(7,7).
hex2dec(8,8).
hex2dec(9,9).
hex2dec(a,10).
hex2dec(b,11).
hex2dec(c,12).
hex2dec(d,13).
hex2dec(e,14).
hex2dec(f,15).

hex2dec(Hex,Dec) :-
atomic(Hex),
atom_chars(Hex,Ac),
reverse(Ca,Ac),!,
convert2dec(1,0,Ca,Dec).

hex2dec(Hex,Dec) :-
atomic(Dec),
convert2hex(Dec,[],Hex).

hex2dec(_,_) :-
write('You must instantiate one of the values for
bigger sets!!'),
nl.

convert2dec(_,Dec,[],Dec) :- !.
convert2dec(F,Tot,[H|T],Dec) :-
F1 is F*16,
name(Hn,[H]),
hex2dec(Hn,Hd),
Tot1 is (Tot + (F * Hd)), !,
convert2dec(F1,Tot1,T,Dec).

convert2hex(Dec,L,[DecHR|L]) :-
Dec < 16,
hex2dec(DecHR,Dec),!.
convert2hex(Dec,Acc,Hex) :-
NewDec is Dec // 16,
Rem is Dec mod 16,
hex2dec(RemX,Rem),!,
convert2hex(NewDec,[RemX|Acc],Hex).
===================
Enjoy!
Mathias Fonkam
==============
Post by Nandini K
Hi Group,
Can u give me some idea on defining a procedure
read_hex_num(X) that reads
hexadecimal numbers and converts them to decimal
numbers.
?- read_hex_num(What).
20
What=32
Yes
?- read_hex_num(What).
10fe
What=4350
Yes
Thanks,
nan
_________________________________________________________________
Post by Nandini K
They're big & powerful. The macho mean machines!
http://server1.msn.co.in/features/suv/index.asp
SUVs are here to stay!
----------------
* To UNSUBSCRIBE, please use the HTML form at
http://www.swi-prolog.org/mailinglist.html
the Subject: "unsubscribe"
(without the quotes) and *no* message body.
** An ARCHIVE of this list is maintained at
http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/
__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com
Richard A. O'Keefe
2003-06-09 04:13:28 UTC
Permalink
There are two ways to write a number in base 16 in SWI Prolog:
The "Prolog" way: 16'F00D. The resemblance to Algol 68 is obvious.
Unfortunately, the ISO Prolog committee were determined to break as
much as they could get away with, and that's not ISO syntax.
The "C" way: 0xF00D. The ISO Prolog committee suffered quite badly
from C envy. Just don't expect C syntax for *octal* to work.

Now, suppose you have Hex = "F00D" as a list of character codes,
and want to convert it to a number. Is there an easy way? Yes:

atom_to_term([0'0,0'x|Hex], Number, _)

will do the job. If there's a leading minus sign, you have to be
a bit cleverer:

hex_to_int(Hex, Int) :-
( Hex = [0'-|Rest] ->
Source = [0'-,0'0,0'x|Rest] % "-F00D" => "-0xF00D"
; Source = [0'0,0'x|Rest] % "FEED" => "0xFEED"
),
atom_to_term(Source, Int, _).



----------------
* To UNSUBSCRIBE, please use the HTML form at

http://www.swi-prolog.org/mailinglist.html

or send mail to prolog-***@swi.psy.uva.nl using the Subject: "unsubscribe"
(without the quotes) and *no* message body.

** An ARCHIVE of this list is maintained at

http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/
Loading...