私はチキンを使用してバイナリデータ形式を読み取っています(fx+ (fxshl (read-byte) 8) (read-byte))
.
フロートを読み書きするにはどうすればよいですか? IEEE 754-2008 の 32 ビットおよび 64 ビットのバイナリ浮動小数点数を読み書きできる必要があります。
私はチキンを使用してバイナリデータ形式を読み取っています(fx+ (fxshl (read-byte) 8) (read-byte))
.
フロートを読み書きするにはどうすればよいですか? IEEE 754-2008 の 32 ビットおよび 64 ビットのバイナリ浮動小数点数を読み書きできる必要があります。
これを行うのに適したライブラリはこれまで見つかりませんでしたが、機能するものを一緒にハックしました。入力操作としてのみ使用できることread-byte
に注意してください。read-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))))