Author Topic: DataToBinary?  (Read 4244 times)

0 Members and 1 Guest are viewing this topic.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
DataToBinary?
« on: April 06, 2009, 12:44:23 AM »
How to Read a Binary File?

For example: I have a BMP file ,now I want to know its data,so I must translate it into a binary list(or a
hexadecimal list). But when I used  "read-line",I got a strange list that was different from "debug" command.

Is it possible? If it's possible,Auto Lisp will be  stronger and more powerful to deal with a lot of problems.
I am a bilingualist,Chinese and Chinglish.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: DataToBinary?
« Reply #1 on: April 06, 2009, 12:48:28 AM »
How to Read a Binary File?

For example: I have a BMP file ,now I want to know its data,so I must translate it into a binary list(or a
hexadecimal list). But when I used  "read-line",I got a strange list that was different from "debug" command.

Is it possible? If it's possible,Auto Lisp will be  stronger and more powerful to deal with a lot of problems.

This thread may help you retrieve the raw data.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: DataToBinary?
« Reply #2 on: April 06, 2009, 01:23:58 AM »
MP,thans for your fast reply.
I read it,I need more time to understande and test.
I am a bilingualist,Chinese and Chinglish.

HofCAD

  • Guest
Re: DataToBinary?
« Reply #3 on: April 06, 2009, 07:43:19 AM »
MP,thans for your fast reply.
I read it,I need more time to understande and test.
For more reading and testing:
http://www.theswamp.org/index.php?topic=17206.0
http://www.theswamp.org/index.php?topic=17265.0

Regards, HofCAD CSI.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: DataToBinary?
« Reply #4 on: April 06, 2009, 09:39:59 AM »
_ReadStream
Code: [Select]
(defun _ReadStream ( path len / fso file stream result )

    ;;  If the file is successful read the data is returned as
    ;;  a string. Won't be tripped up by nulls, control chars
    ;;  including ctrl z (eof marker). Pretty fast (feel free
    ;;  to bench mark / compare to alternates).
    ;;
    ;;  If the caller wants the result as a list of byte values
    ;;  simply use vl-string->list on the result:
    ;;
    ;;      (setq bytes
    ;;          (if (setq stream (_ReadStream path len))
    ;;              (vl-string->list stream)
    ;;          )
    ;;      )
    ;;
    ;;  Arguments:
    ;;
    ;;      path  <duh>
    ;;      len   Number of bytes to read. If non numeric, less
    ;;            than 1 or greater than the number of bytes in
    ;;            the file everything is returned.

    (vl-catch-all-apply
       '(lambda ( / iomode format size )
            (setq
                iomode  1 ;; 1 = read,   2 = write,    8 = append
                format  0 ;; 0 = ascii, -1 = unicode, -2 = system default
                fso     (vlax-create-object "Scripting.FileSystemObject")
                file    (vlax-invoke fso 'GetFile path)
                stream  (vlax-invoke fso 'OpenTextFile path iomode format)
                size    (vlax-get file 'Size)
                len     (if (and (numberp len) (< 0 len size)) (fix len) size)
                result  (vlax-invoke stream 'read len)
            )
            (vlax-invoke stream 'Close)
        )
    )

    (if stream (vlax-release-object stream))
    (if file (vlax-release-object file))
    (if fso (vlax-release-object fso))

    result

)

_IntToHex
Code: [Select]
(defun _IntToHex ( int / codes hex )

    (setq
        int    (fix int)
        codes '(48 49 50 51 52 53 54 55 56 57 65 66 67 68 69 70)
    )

    (while (< 0 (fix int))
        (setq
            hex (cons (nth (fix (rem int 16.0)) codes) hex)
            int (/ int 16.0)
        )
    )

    (if hex (vl-list->string hex) "0")

)

_RSet
Code: [Select]
(defun _RSet ( str white len )

    ;;  right justify str, padding the result with
    ;;  white chars for a total string ength of len
    ;;
    ;;  (_RSet "right" "_" 10) => "_____right"

    (while (< (strlen white) len) (setq white (strcat white white)))
   
    (substr (setq str (strcat white str)) (- (strlen str) len -1))

)

Quick and Dirty c:Test
Code: [Select]
(defun c:Test ( / filename stream )

    (if (setq filename (getfiled "Select bitmap image:" "c:\\" "bmp" 128))
        (if (setq i 0 stream (_ReadStream filename nil))
            (foreach byte (vl-string->list stream)
                (princ
                    (strcat
                        (_RSet (_IntToHex byte) "0" 2)
                        (cond
                            ((zerop (rem (setq i (1+ i)) 16)) "\n")
                            ((zerop (rem i 8)) " | ")
                            (" ")
                        )
                    )
                )                 
            )
            (princ
                (strcat
                    "Error attempting to read data from <"
                    filename
                    ">.\n"
                )
            )               
        )
    )
   
    (princ)
   
)

c:Test's output assuming a bitmap was selected ...

42 4D 06 39 00 00 00 00 | 00 00 36 04 00 00 28 00
00 00 66 00 00 00 82 00 | 00 00 01 00 08 00 00 00
00 00 D0 34 00 00 00 00 | 00 00 00 00 00 00 00 01
00 00 00 01 00 00 9A 0C | 0F 00 00 00 00 00 11 14
17 00 39 A9 6F 00 BB BE | E8 00 87 77 4F 00 BB A2
6A 00 81 71 3C 00 83 7B | E6 00 3C 50 CC 00 47 AF
B4 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 ...
« Last Edit: April 06, 2009, 09:50:55 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: DataToBinary?
« Reply #5 on: April 06, 2009, 11:24:46 AM »
MP,Thanks for your wonderful program! I tried it,and got my need.
HofCAD,Thanks for your links.I will study from them.
I am a bilingualist,Chinese and Chinglish.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: DataToBinary?
« Reply #6 on: April 06, 2009, 11:36:13 AM »
You're most welcome HFB, glad you found what you needed.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

xshrimp

  • Mosquito
  • Posts: 14
Re: DataToBinary?
« Reply #7 on: April 07, 2009, 10:45:36 AM »
how convert Binarydata "424D0639000000000000360400............" to file??
 :-(
abc

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: DataToBinary?
« Reply #8 on: April 07, 2009, 10:50:56 AM »
Assuming that's a stream of hex data ...

1. Parse it into a list of 2 character strings.
2. Apply a HexToInt function to each element.
3. Use vl-list->string to convert to a string.
4. Write to file using _WriteStream.
5. Have a cookie.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: DataToBinary?
« Reply #9 on: April 08, 2009, 08:37:04 AM »
You're most welcome HFB, glad you found what you needed.

:)
Ha,by the way , I want to know what's "HFB"? I asked some foreigners( English teachers),they didn't know its meaning either.
I am a bilingualist,Chinese and Chinglish.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: DataToBinary?
« Reply #10 on: April 08, 2009, 08:44:59 AM »
You're most welcome HFB, glad you found what you needed.

:)
Ha,by the way , I want to know what's "HFB"? I asked some foreigners( English teachers),they didn't know its meaning either.
your name dude. High Fly Bird

thanks for this michael, i was wondering how to achieve this the other day.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: DataToBinary?
« Reply #11 on: April 08, 2009, 08:47:56 AM »
It's a secret acronym:

HFB = HighFlyBird.

:D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: DataToBinary?
« Reply #12 on: April 08, 2009, 09:08:17 AM »
your name dude. High Fly Bird

Sorry, didn't see you had responded (blackberry screen is kinda small, ez to miss stuff).

thanks for this michael, i was wondering how to achieve this the other day.

My pleasure AJT. :D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: DataToBinary?
« Reply #13 on: April 08, 2009, 09:09:45 AM »
Oh,It's funny.  I know a song that its name is "high-flying bird", (Music by Elton John ,Lyrics by Bernie Taupin).
Actually,I want to write a lisp to draw in CAD by a picture, so I've to know how to read a binary file. But now I have no free time,so maybe after a few days, I'll think about it.
Is there any suggestions?
« Last Edit: April 08, 2009, 09:13:41 AM by highflybird »
I am a bilingualist,Chinese and Chinglish.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8781
  • AKA Daniel
Re: DataToBinary?
« Reply #14 on: April 08, 2009, 09:15:34 AM »
Cool! I had tried writing slide files once before but I gave up  :mrgreen:
http://www.theswamp.org/index.php?topic=17265.0