Author Topic: How to write a 32-bit number into a file  (Read 2165 times)

0 Members and 1 Guest are viewing this topic.

baitang36

  • Bull Frog
  • Posts: 213
How to write a 32-bit number into a file
« on: August 23, 2020, 09:34:50 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun tt ()
  2.   (setq aa (open "d:/tt.txt" "w"))
  3.   (write-long 943208504 aa)
  4.   (close aa)
  5. )
  6. (tt)

It is wrong to write in this way. How can we make it not wrong?

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: How to write a 32-bit number into a file
« Reply #1 on: August 23, 2020, 10:10:19 AM »
(write-line (itoa 943208504) aa)

Or, for integers greater than 2,147,483,647 (231 - 1):

(write-line (rtos 943208504 2 0) aa)

ribarm

  • Gator
  • Posts: 3265
  • Marko Ribar, architect
Re: How to write a 32-bit number into a file
« Reply #2 on: August 23, 2020, 11:38:26 AM »
Just to inform, FYI...
(rtos num 2 0) can give undesired results when number is enormously large, i.e. (around 1e+15 or 1e+16)... Test it with some still writable number that don't exceed numerical notation...

We can conclude - everything you do in ACAD, you must do in some legit limits (neither too big numbers nor too small - 1e-15) - no wonder that many have asked why for ex. IntersectWith method don't find intersections - their DWGs contained reference curves far away from 0,0,0 origin point (1e+10 or similar)...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

d2010

  • Bull Frog
  • Posts: 326
Re: How to write a 32-bit number into a file
« Reply #3 on: August 23, 2020, 12:03:41 PM »
Or  How to write directly ?
(Setq dectoint 0xFFAFEAD)
(Setq LeeMac (+ 0xFFAFEAD 0x1FAFEAD))
(Alert "Can you use hexadecimals without convert Dec2Int?")
 :mrgreen:

(write-line (itoa 943208504) aa)
Or, for integers greater than 2,147,483,647 (231 - 1):
(write-line (rtos 943208504 2 0) aa)
« Last Edit: August 26, 2020, 08:46:54 AM by d2010 »

baitang36

  • Bull Frog
  • Posts: 213
Re: How to write a 32-bit number into a file
« Reply #4 on: August 23, 2020, 10:20:31 PM »
Thank you, Lee Mac ,d2010 and ribarm
I want to write binary files directly.
The write-long function exists, but LSP cannot call it directly.
 I'm trying to make it work.

baitang36

  • Bull Frog
  • Posts: 213
Re: How to write a 32-bit number into a file
« Reply #5 on: August 25, 2020, 05:44:09 PM »
Or  How to write directly ?
(Setq dectoint 0xFFAFEAD)
(Setq LeeMac (+ 0xFFAFEAD 0x1FAFEAD))
(Alert "Can you user hexadecimals without convert Dec2Int?")
 :mrgreen:

(write-line (itoa 943208504) aa)
Or, for integers greater than 2,147,483,647 (231 - 1):
(write-line (rtos 943208504 2 0) aa)

My experiment is only half successful, and I can realize this idea in FAS. But LSP cannot be implemented。
Please see the attachment

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: How to write a 32-bit number into a file
« Reply #6 on: August 26, 2020, 03:14:52 AM »
In BricsCAD this works;
Code - Auto/Visual Lisp: [Select]
  1. (defun tt ()
  2.   (setq aa (open "d:/tt.bin" "wb"))
  3.   (vle-write-int32-be aa 943208504)
  4.   (close aa)
  5. )

Note the "wb" mode means "write binary" and is undocumented but supported by AutoLISP as well apparently.

In AutoCAD it should be possible to split the 32bit integer into 4 bytes and then write these.
http://www.theswamp.org/index.php?topic=17465.0
http://www.theswamp.org/index.php?topic=41802.10
« Last Edit: August 26, 2020, 03:19:00 AM by roy_043 »

d2010

  • Bull Frog
  • Posts: 326
Re: How to write a 32-bit number into a file
« Reply #7 on: August 26, 2020, 05:48:57 AM »
Quote from: Lee Mac [/quote
My experiment is only half successful, and I can realize this idea in FAS. But LSP cannot be implemented。
Please see the attachment
For my Source/s is okay. Thank you,Lee Mac for your-answer. :woow:
« Last Edit: August 26, 2020, 06:29:08 AM by d2010 »

baitang36

  • Bull Frog
  • Posts: 213
Re: How to write a 32-bit number into a file
« Reply #8 on: August 27, 2020, 01:11:37 AM »
In BricsCAD this works;
Code - Auto/Visual Lisp: [Select]
  1. (defun tt ()
  2.   (setq aa (open "d:/tt.bin" "wb"))
  3.   (vle-write-int32-be aa 943208504)
  4.   (close aa)
  5. )

Note the "wb" mode means "write binary" and is undocumented but supported by AutoLISP as well apparently.

In AutoCAD it should be possible to split the 32bit integer into 4 bytes and then write these.
http://www.theswamp.org/index.php?topic=17465.0
http://www.theswamp.org/index.php?topic=41802.10
thank you

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
A man who never made a mistake never made anything