[racket-dev] hex decoding?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Tue Jun 11 16:33:47 EDT 2013


 db/private/mysql/connection.rkt does not export the function, otherwise you could. 


On Jun 11, 2013, at 4:26 PM, David Vanderson wrote:

> 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
> 
> _________________________
> Racket Developers list:
> http://lists.racket-lang.org/dev



Posted on the dev mailing list.