[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Converting numbers into sequences of bytes

William James

8/8/2015 5:16:00 AM

Bjorn Lindberg wrote:

> Here is a function to turn an integer into an array of high-to-low
> order bytes:
>
> (defun integer->bytes (n)
> (loop for i = n then quot
> for (quot rem) = (multiple-value-list (floor i 256))
> collect rem into result
> while (> quot 0)
> finally (return (make-array (length result)
> :element-type '(unsigned-byte 8)
> :initial-contents (nreverse result)))))
>
> (integer->bytes (expt 2 32))
> ==> #(1 0 0 0 0)
> (integer->bytes (1- (expt 2 32)))
> ==> #(255 255 255 255)

Gauche Scheme:

(use srfi-1 :only (unfold-right))

(define (integer->bytes n)
(list->vector
(unfold-right zero? (cut mod <> 256) (cut div <> 256) n)))


(integer->bytes (expt 2 32))
===>
#(1 0 0 0 0)

(integer->bytes (- (expt 2 32) 1))
===>
#(255 255 255 255)

--
[W]e had enough employees who made more than 85 to fill all the openings. The
highest score that any of the blacks scored on the test was 11. The lowest
score that any black made on the test was 4. All four of those blacks went
into skilled-trades training.
https://archive.org/download/TheOldmanArchives/oldman29-...