TEXT   82
procedure hex
Guest on 12th March 2023 09:14:45 AM


  1. procedure hex(s)
  2.  
  3.    local a,c
  4.  
  5.    a := 0
  6.    every c := !map(s) do
  7.        a := ior(find(c,"0123456789abcdef") - 1, ishift(a,4)) | fail
  8.    return a
  9.  
  10. end
  11. #
  12. #
  13. procedure hexstring(i,n,lowercase)
  14.  
  15.    local s, hexchars, sign
  16.  
  17.    i := integer(i) | runerr(101, i)
  18.    sign := ""
  19.    if i = 0 then s := "0"
  20.    else {
  21.        if /n & i < 0 then {
  22.            sign := "-"
  23.            i := -i
  24.        }
  25.        hexchars := if \lowercase
  26.        then "0123456789abcdef"
  27.        else "0123456789ABCDEF"
  28.        s := ""
  29.        until i = (0 | -1) do {
  30.            s := hexchars[iand(i,15) + 1] || s
  31.            i := ishift(i,-4)
  32.        }
  33.    }
  34.    if \n > *s then s := right(s,n,if i >= 0 then "0" else hexchars[16])
  35.  
  36.    return sign || s
  37.  
  38. end

Raw Paste

Login or Register to edit or fork this paste. It's free.