[racket-dev] hex decoding?

From: David Vanderson (david.vanderson at gmail.com)
Date: Tue Jun 11 16:26:46 EDT 2013

Thank you Stephen and Tony for your examples.  I found the following 
private function in db/private/mysql/connection.rkt:

(define (hex-string->bytes s)
   (define (hex-digit->int c)
     (let ([c (char->integer c)])
       (cond [(<= (char->integer #\0) c (char->integer #\9))
              (- c (char->integer #\0))]
             [(<= (char->integer #\a) c (char->integer #\f))
              (+ 10 (- c (char->integer #\a)))]
             [(<= (char->integer #\A) c (char->integer #\F))
              (+ 10 (- c (char->integer #\A)))])))
   (unless (and (string? s) (even? (string-length s))
                (regexp-match? #rx"[0-9a-zA-Z]*" s))
     (raise-type-error 'hex-string->bytes
                       "string containing an even number of hexadecimal 
digits" s))
   (let* ([c (quotient (string-length s) 2)]
          [b (make-bytes c)])
     (for ([i (in-range c)])
       (let ([high (hex-digit->int (string-ref s (+ i i)))]
             [low  (hex-digit->int (string-ref s (+ i i 1)))])
         (bytes-set! b i (+ (arithmetic-shift high 4) low))))
     b))


Can this function be exported?  I'm willing to make a patch with docs 
and tests - is file/sha1 the right place?

Thanks,
Dave


Posted on the dev mailing list.