2011-02-03 3 views
1

치킨을 사용하여 이진 데이터 형식을 읽고 있는데, 지금까지 (fx+ (fxshl (read-byte) 8) (read-byte)) (Big Endian)과 같은 작업을 수행하여 int를 처리했습니다.치킨 스킴으로 이진 수레를 읽고 쓸 수 있습니까?

어떻게 수레를 읽고 쓸 수 있습니까? IEEE 754-2008 32 비트 및 64 비트 이진수 부동 소수점을 읽고 쓸 수 있어야합니다.

답변

0

나는 지금까지 이것을 수행 할만한 좋은 라이브러리를 찾지 못했지만 작동하는 것을 함께 해킹했습니다. 입력 작업으로 read-byteread-string 만 사용할 수 있습니다.

;; 
    ;; These are some unfun C routines to convert 4 int-promoted bytes to a float 
    ;; by manually assembling the float using bitwise operators 
    ;; 
    ;; Caveat! These will only work on platforms in which floats are 32-bit Big 
    ;; Endian IEEE754-2008 numbers and doubles are 64-bit Big Endian IEEE754-2008 
    ;; numbers! Also, stdint.h. 
    ;; 
    (define (readFloat) 
    (let ([c-read-float 
      (foreign-lambda* float 
       ((int i1) 
       (int i2) 
       (int i3) 
       (int i4)) 
       "uint8_t b1 = (uint8_t) i1; 
       uint8_t b2 = (uint8_t) i2; 
       uint8_t b3 = (uint8_t) i3; 
       uint8_t b4 = (uint8_t) i4; 

       uint32_t i = 0; 

       i = b1; 
       i = (i << 8) | b2; 
       i = (i << 8) | b3; 
       i = (i << 8) | b4; 

       float f = *(float*)&i; 

       C_return(f);")]) 
     (let* ([i1 (read-byte)] 
       [i2 (read-byte)] 
       [i3 (read-byte)] 
       [i4 (read-byte)]) 
      (c-read-float i1 i2 i3 i4)))) 
+0

[bitstring egg] (http://wiki.call-cc.org/eggref/4/bitstring)가 제공되므로 훨씬 간단 해집니다. – sjamaan

관련 문제