Skip to content

Instantly share code, notes, and snippets.

@NalaGinrut
Created October 19, 2012 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NalaGinrut/3917036 to your computer and use it in GitHub Desktop.
Save NalaGinrut/3917036 to your computer and use it in GitHub Desktop.
string->binary
(use-modules (rnrs))
(define* (string->binary str #:key (base 16) (endiannes 'little))
(let ((n (string->number str base))
(f (case endiannes
((big) identity)
((little) reverse)
(else (error "wrong endiannes" endiannes)))))
(let lp ((n n) (result '()))
(if (zero? n)
(list->u8vector (f result))
(lp (ash n -8) (cons (logand n #xff) result))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment