caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Miles Egan <miles@caddr.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] printable digest strings
Date: Fri, 4 May 2001 11:37:27 +0200	[thread overview]
Message-ID: <20010504113727.A9728@pauillac.inria.fr> (raw)
In-Reply-To: <20010503153234.A28259@caddr.com>; from miles@caddr.com on Thu, May 03, 2001 at 03:32:34PM -0700

> 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?

It does not exist, but as you said it could be handy.

> 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:

You can use Printf.sprintf "%02x" to convert each byte to hexadecimal
in a more concise way.  E.g.:

let string_map f s =            
  let rec map_aux res idx =
    if idx < 0 then res else map_aux (f s.[idx] :: res) (idx - 1)  
  in map_aux [] (String.length s - 1)

let hexstring s =
  String.concat "" 
    (string_map (fun c -> Printf.sprintf "%02x" (Char.code c)) s)

Or in a more imperative style (real programmers don't build
intermediate lists :-)

let hexstring s =
  let res = String.create (String.length s * 2) in
  for i = 0 to String.length s - 1 do
    String.blit (Printf.sprintf "%02x" (Char.code s.[i])) 0
                res (2 * i) 2
  done;
  res

Hope this helps,

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


  parent reply	other threads:[~2001-05-04  9:37 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-05-03 22:32 Miles Egan
2001-05-04  9:15 ` Hendrik Tews
2001-05-04  9:37 ` Xavier Leroy [this message]
2001-05-04 14:00   ` Miles Egan
2001-05-04 14:50     ` Brian Rogoff
2001-05-04 20:27   ` Chris Hecker
2001-05-04 22:54     ` Miles Egan
2001-05-06 18:56     ` [Caml-list] Wserver: Values of global variables lost Mattias Waldau
2001-05-07  8:58       ` Daniel de Rauglaudre
2001-05-07 17:00       ` Mattias Waldau
2001-05-07 18:16         ` Daniel de Rauglaudre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010504113727.A9728@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=miles@caddr.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).