summaryrefslogtreecommitdiff
path: root/ipl/procs/hexcvt.icn
blob: 4da5e3157f6421a8d11cb69354048abcfa2faa65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
############################################################################
#
#	File:     hexcvt.icn
#
#	Subject:  Procedures for hexadecimal conversion
#
#	Author:   Robert J. Alexander
#
#	Date:     June 7, 1994
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  hex(s) -- Converts string of hex digits into an integer.
#
#  hexstring(i,n,lc) -- Returns a string that is the hexadecimal
#  representation of the argument.  If n is supplied, a minimum
#  of n digits appear in the result; otherwise there is no minimum,
#  and negative values are indicated by a minus sign.  If lc is
#  non-null, lowercase characters are used instead of uppercase. 
#
############################################################################

procedure hex(s)
   local a,c
   a := 0
   every c := !map(s) do
	 a := ior(find(c,"0123456789abcdef") - 1,ishift(a,4)) | fail
   return a
end

procedure hexstring(i,n,lowercase)
   local s,hexchars,sign
   i := integer(i) | runerr(101,i)
   sign := ""
   if i = 0 then s := "0"
   else {
      if /n & i < 0 then {
         sign := "-"
         i := -i
         }
      hexchars := if \lowercase then "0123456789abcdef" else "0123456789ABCDEF"
      s := ""
      until i = (0 | -1) do {
         s := hexchars[iand(i,15) + 1] || s
         i := ishift(i,-4)
         }
      }
   if \n > *s then s := right(s,n,if i >= 0 then "0" else hexchars[16])
   return sign || s
end