Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] printable digest strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Miles Egan <miles@c...>
Subject: [Caml-list] printable digest strings
I can't seem to find a function to create printable versions of the digest
strings generated by the digest module, like the output of the common unix
md5sum utility.  Am I missing something or does it not exist?  If not, it might
be a handy addition to the Digest module.  At the moment I'm using this fairly
gross code of my own:

let hexchar nibble =
  let ac = Char.code 'a' in
  let zc = Char.code '0' in
  if nibble > 9 then
    Char.chr (nibble + ac - 10)
  else
    Char.chr (nibble + zc)

let hexstring s =
  let
    j = ref 0 and
    h = String.create (String.length s * 2) in
    for i = 0 to (String.length s - 1) do
      let
        high = ((Char.code s.[i]) lsr 4) land 0xf and
        low = (Char.code s.[i]) land 0xf in
        let _ = h.[!j] <- hexchar high in
        let _ = j := !j + 1 in
        let _ = h.[!j] <- hexchar low in
        j := !j + 1
    done;
    h

-- 
miles
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr