main
Matthew Butterick 6 years ago
parent f7ff697540
commit 79fc919ee6

@ -8,16 +8,7 @@
(cond
[(bytes? arg) (open-input-bytes arg)]
[(input-port? arg) arg]
[else (raise-argument-error '->port "byte string or input port" arg)]))
(define (reverse-bytes bstr) (apply bytes (reverse (bytes->list bstr))))
(define (unsigned->signed uint bits)
(define most-significant-bit-mask (arithmetic-shift 1 (sub1 bits)))
(- (bitwise-xor uint most-significant-bit-mask) most-significant-bit-mask))
(define (signed->unsigned sint bits)
(bitwise-and sint (arithmetic-shift 1 bits)))
[else (raise-argument-error '->input-port "byte string or input port" arg)]))
(define (dump x)
(cond

@ -7,6 +7,17 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
|#
(define (unsigned->signed uint bits)
(define most-significant-bit-mask (arithmetic-shift 1 (sub1 bits)))
(- (bitwise-xor uint most-significant-bit-mask) most-significant-bit-mask))
(define (signed->unsigned sint bits)
(bitwise-and sint (arithmetic-shift 1 bits)))
(define (reverse-bytes bstr)
(apply bytes
(for/list ([b (in-bytes bstr (sub1 (bytes-length bstr)) -1 -1)])
b)))
(define (exact-if-possible x) (if (integer? x) (inexact->exact x) x))

Loading…
Cancel
Save