How can I convert double-float to byte array and vice versa in common lisp?

chunsj

For integer (32bit or 4 bytes) I can do as follows;

(defun get-u4 (arr pos)
  (let ((u4 0))
    (setf (ldb (byte 8 0) u4) (aref arr pos))
    (setf (ldb (byte 8 8) u4) (aref arr (+ pos 1)))
    (setf (ldb (byte 8 16) u4) (aref arr (+ pos 2)))
    (setf (ldb (byte 8 24) u4) (aref arr (+ pos 3)))
  u4))

(defun put-u4 (arr pos int)
  (setf (aref arr pos) (ldb (byte 8 0) int))
  (setf (aref arr (+ pos 1)) (ldb (byte 8 8) int))
  (setf (aref arr (+ pos 2)) (ldb (byte 8 16) int))
  (setf (aref arr (+ pos 3)) (ldb (byte 8 24) int)))

However, I cannot figure out how can I do this for 64bit or 8byte double-float? Little-Endianess is assumed.

=============

I found one solution (using external library); https://www.quicklisp.org/beta/UNOFFICIAL/docs/ieee-floats/doc/index.html using this one, I can encode/decode double-float to/from integer with proper size.

anquegi

From wikipedia we get this:

enter image description here

enter image description here

You can get the library you mentioned in the comment with quicklisp:

CL-USER> (ql:quickload 'ieee-floats)
To load "ieee-floats":
  Install 1 Quicklisp release:
    ieee-floats
; Fetching #<URL "http://beta.quicklisp.org/archive/ieee-floats/2015-06-08/ieee-floats-20150608-git.tgz">
; 4.92KB
==================================================
5,041 bytes in 0.01 seconds (378.68KB/sec)
; Loading "ieee-floats"
[package ieee-floats]
(IEEE-FLOATS)
CL-USER> (ieee-floats:encode-float32 23d2)
1158660096
CL-USER> (ieee-floats:decode-float32 #b010101)
2.9427268e-44
CL-USER> (ieee-floats:encode-float32 0)
; Evaluation aborted on #<TYPE-ERROR expected-type: FLOAT datum: 0>.
CL-USER> (ieee-floats:encode-float32 0.0)
0
CL-USER> (ieee-floats:encode-float32 0.1)
1036831949

you can use of course iee-floats:encode/decode-float64 of course,

You also need to know that #b010101 is a macro that represents the number in binary you can use in normal math with common lisp:

CL-USER> (+ 2 #b10)
4

so it converts the binary for you to an integer, then you can also have the utility with format

(format nil "~B" 2)
"10"

That you can use to convert integers to binary string, finally the only thing that you may take care is the 0 autocompletion before the number in binary, the ieee library removes them for the representation

CL-USER> (ieee-floats:encode-float64 1.0d0)
4607182418800017408
CL-USER> (format nil "~B" *)
"11111111110000000000000000000000000000000000000000000000000000"
CL-USER> (length *)
62
CL-USER> (ieee-floats:encode-float64 -1.0d0)
13830554455654793216
CL-USER> (format nil "~B" *)
"1011111111110000000000000000000000000000000000000000000000000000"
CL-USER> (length *)
64

CL-USER> (ieee-floats:decode-float64 #b11111111110000000000000000000000000000000000000000000000000000 )
1.0d0
CL-USER> (ieee-floats:decode-float64 #b1011111111110000000000000000000000000000000000000000000000000000 )
-1.0d0
CL-USER> (ieee-floats:decode-float64 #b0011111111110000000000000000000000000000000000000000000000000000 )
1.0d0

With this you can make all your needed transformations, good luck

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert a float into a byte array and vice versa?

How to convert byte array to string and vice versa?

How to convert double byte character/string to single byte and vice versa?

How do you convert a byte array to a hexadecimal string, and vice versa?

Convert a byte array to integer in Java and vice versa

Convert file to byte array and vice versa

How can I convert NSDictionary to NSData and vice versa?

How can I convert tabs to spaces and vice versa in an existing file

How can I convert a Unix timestamp to DateTime and vice versa?

How to convert numpy array to string and vice versa

Cannot convert String to byte array and vice versa in Java

How to convert video/audio file to byte array and vice versa in android.?

BitmapImage to byte array and vice versa

Converting Double to Float and vice versa manually

Java: Can I convert List of Object to List of String[] and vice versa?

Convert double number to digits and vice versa?

How to convert string into numpy array for tensorflow and vice versa

Put byte array to JSON and vice versa

Converting String to Byte Array and vice versa

How can I convert char to symbol in common lisp?

How to convert float into byte array?

What is the most efficient way to convert array of int to array of byte and vice versa?

How can I change upper case to lower and vice versa?

How can I implement vice versa mapping in Python?

How can I successfully lookup hostname from an IP, but not vice versa?

How can I switch a public repo to private and vice versa on GitHub?

In Java, how can I convert an InputStream into a byte array (byte[])?

Convert struct array to cell of structs and vice versa

ByteBuffer - convert long to char array and vice versa