Convert binary file to numbers

Convert binary file to numbers

autoid374ceb4990
Collaborator Collaborator
417 Views
9 Replies
Message 1 of 10

Convert binary file to numbers

autoid374ceb4990
Collaborator
Collaborator

I have a file that is composed of integers and float numbers in binary format.  The integers and floats are each 4 bytes in length.  Using LISP is there a way to convert the binary code to ASCII numbers?

0 Likes
418 Views
9 Replies
Replies (9)
Message 2 of 10

Kent1Cooper
Consultant
Consultant

Do you mean binary to base-10?  Some Searching may find a routine to do that.

Or binary to ASCII character codes for numerical text characters?  Something else?

What kind of file is it?  [A small sample file would help.]

Kent Cooper, AIA
0 Likes
Message 3 of 10

autoid374ceb4990
Collaborator
Collaborator

It is a binary STL file (see attached).  The first 80 bytes are the header.  The numbers ( 4 byte floats) begin at byte 81.  I can easily read the numbers in C code with this line of code: fread(&x,sizeof(x),1,in_ptr), x is defined as a float.  I have a LISP program to convert the ASCII STL files to 3DFACES, and I am trying to do the same with the binary STL file, but I am not a very good LISP programmer.

0 Likes
Message 4 of 10

Kent1Cooper
Consultant
Consultant

That's beyond me.  But some thoughts:

AutoLisp has an (open) function that can make certain file types usable by its other functions like (read-line).  You should read about those.

I don't know whether (open) can take a .stl file.  Do you have any way of exporting or converting it to plain text [.txt] or a spreadsheet [.csv] file types?

Kent Cooper, AIA
0 Likes
Message 5 of 10

Sea-Haven
Mentor
Mentor

I used Copilot 1st and got something search "Read binary file autolisp" the autolisp is important got an answer to long winded to post here.

 

Ok second go tried Gemini by Google got a better answer. Made a list of test file.

(defun c:ReadBinaryFile ( / fObject filePath streamData bytesList)
  (vl-load-com)
  
  ;; Specify your file path here
  (setq filePath "C:\\path\\to\\your\\file.bin")

  ;; 1. Create the ADO Stream Object
  (setq fObject (vlax-create-object "ADODB.Stream"))
  
  ;; 2. Configure and Open
  (vlax-put-property fObject 'Type 1) ; 1 = adTypeBinary
  (vlax-invoke-method fObject 'Open)
  (vlax-invoke-method fObject 'LoadFromFile filePath)
  
  ;; 3. Read the entire file
  (setq streamData (vlax-invoke-method fObject 'Read))
  
  ;; 4. Convert the SafeArray to a standard LISP list
  (if streamData
    (setq bytesList (vlax-safearray->list (vlax-variant-value streamData)))
  )

  ;; Clean up
  (vlax-invoke-method fObject 'Close)
  (vlax-release-object fObject)

  ;; Return the list of bytes (or nil if failed)
  bytesList
)

That is as far as I can go with help. All yours now.

0 Likes
Message 6 of 10

autoid374ceb4990
Collaborator
Collaborator

Kent1Cooper:

Yes, I have an ARX program that can convert the binary STL file to an ASCII STL file, but I was looking for an all LISP method.

 

Sea-Haven:

Thanks for the LISP file, but unfortunately I have only an old version of AutoCAD (R14) and the visual lisp is not supported.

0 Likes
Message 7 of 10

Kent1Cooper
Consultant
Consultant

@autoid374ceb4990 wrote:

It is a binary STL file (see attached).  ....


AutoLisp cannot (open) that, and therefore I assume cannot read from it.  And I tried opening it in Notepad, to see if it's some kind of plain-text format, but I got only gobbledegook -- mostly Asian-language-looking but also other kinds of characters, but no numbers, binary or otherwise.

junk.png

Kent Cooper, AIA
0 Likes
Message 8 of 10

Kent1Cooper
Consultant
Consultant

@autoid374ceb4990 wrote:

.... I have only an old version of AutoCAD (R14) ....


Not that it makes any difference, but I'm curious:  Do you mean AutoCAD 2014, or do you, in fact, actually mean Release 14 from -- I'm not sure -- over 25 years ago?

Kent Cooper, AIA
0 Likes
Message 9 of 10

autoid374ceb4990
Collaborator
Collaborator

If you try to open a binary STL file in notepad you will get something that looks like this:

binarySTL.jpg

An ASCII STL file will look like this:

solid OpenSCAD_Model
facet normal 0 0 -1
outer loop
vertex 242.67 235.053 0
vertex 242.801 236.271 0
vertex 242.976 235.619 0
endloop
endfacet

 

So writing a LISP program to plot the triangles in the STL file is fairly easy.

 

And yes, AutoCAD R14.  Years ago I was an AutoCAD "developer" and received all of AutoDesk's programs free (Inventor crashed every time I tried it).  The developer fee was @$200 (I think) per year.  Then AutoDesk got greedy and raised the developer price higher that I was willing to pay and they demanded that you have so many software sales per year, plus you had to purchase a new C/C++ compiler ever time a new AutoCAD version came out, and I did not care for their subscription rental policy either.  I used AutoCad in my land surveying business, mainly to do field computations and draw maps and I could program R14 to do everything I needed, so I never saw the need to upgrade.

Message 10 of 10

Sea-Haven
Mentor
Mentor

Wow I remember R14, my reference books paper copies all 5 off them are R12 paper copies. So much better sometimes than electronic help.

 

Sent you a PM.

0 Likes