Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```scheme
- (def (u8vector->uint bv (guard #t))
- (def (compute bv)
- (let ((l (fx- (u8vector-length bv) 1)))
- (let loop ((s 0) (i 0))
- (if (< i l)
- (loop (+ s (* (u8vector-ref bv i) (expt 256 (- l i)))) (+ i 1))
- (+ s (* (u8vector-ref bv i) (expt 256 (- l i))))))))
- (if guard
- (if (fx<= (u8vector-length bv) 8)
- (compute bv)
- (error "Disable guard to compute on u8vectors of length > 8." bv))
- (compute bv)))
- ```
- ``` scheme
- > (u8vector->uint #u8(0 1))
- 1
- > (u8vector->uint (make-u8vector 2 #xFF))
- 65535
- > (u8vector->uint (make-u8vector 8 #xFF))
- 18446744073709551615
- > (equal? (- (expt 2 64) 1) (u8vector->uint (make-u8vector 8 #xFF)))
- #t
- > (u8vector->uint (make-u8vector 10 #xFF))
- *** ERROR IN (console)@7.1 -- Disable guard to compute on u8vectors of length > 8. #u8(255 255 255 255 255 255 255 255 255 255)
- > (u8vector->uint (make-u8vector 10 #xFF) #f)
- 1208925819614629174706175
- > (equal? (- (expt 2 80) 1) (u8vector->uint (make-u8vector 10 #xFF) #f))
- #t
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement