明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3156|回复: 9

[基础] 读写二进制研究

[复制链接]
发表于 2009-12-28 15:17 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2009-12-28 23:28:31 编辑

;Subject:binary conversion to text by lisp 二进制文件转文本
;Writen:nonsmall(不死猫)
;Date:2009 10 18
;All Rights Reserved 版权所有 nonsmall(不死猫)
;Contact: QQ:43797405 Email:nonsmall@163.com
(defun binaryToText(FileName / ADODB.Stream)
 (Setq ADODB.Stream (Vlax-Get-Or-Create-Object "ADODB.Stream" ))
 (Vlax-Put-Property ADODB.Stream 'Type 1 )
 (Vlax-Invoke ADODB.Stream 'Open )
 (Vlax-Invoke-Method ADODB.Stream 'LoadFromFile FileName )
 (Vlax-Put-Property ADODB.Stream 'Position 0 )
 (Vlax-Invoke-Method ADODB.Stream 'Write (car (list (Vlax-Invoke-Method ADODB.Stream 'Read (Vlax-Get ADODB.Stream 'Size )) (Vlax-Put-Property ADODB.Stream 'Position 0 ))))
 (Vlax-Put-Property ADODB.Stream 'Position 0 )
 (Vlax-Put-Property ADODB.Stream 'Type 2 ) 
 (Vlax-Put-Property ADODB.Stream 'CharSet "us-ascii" )
 (Vlax-Invoke ADODB.Stream 'ReadText)
 ;(vlax-release-object ADODB.Stream)
)

(defun _WriteStream ( path text mode / fso stream file result )

    ;;  Return the file size if the file is successfully written
    ;;  to, otherwise nil. Will write all ascii chars to file
    ;;  including nulls. If the caller wants to pass a list of
    ;;  byte values to the function just call it like so:
    ;;
    ;;      (_WriteStream
    ;;          path
    ;;          (vl-list->string '(87 111 111 116 33))
    ;;          mode
    ;;      )
    ;;
    ;;  Arguments:
    ;;
    ;;      path  <duh>
    ;;      text  <duh>
    ;;      mode  "a" to create/append,
    ;;            "w" to create/overwrite (default)
   
    (setq mode (if (member mode '("a" "A")) "a" "w"))
   
    (vl-catch-all-apply
       '(lambda ( / format )
            (setq fso (vlax-create-object "Scripting.FileSystemObject"))
            (cond
                (   (or (null (findfile path)) (eq "w" mode))
                    (setq stream
                        (vlax-invoke
                            fso
                           'CreateTextFile
                            path
                           -1 ;; 0 (false) = don't overwrite , -1 (true) = overwrite
                            0 ;; 0 (false) = ascii, -1 (true) = unicode
                        )
                    )
                    (setq file (vlax-invoke fso 'GetFile path))
                )
                (   (setq file (vlax-invoke fso 'GetFile path))
                    (setq stream
                        (vlax-invoke
                            file
                           'OpenAsTextStream
                            8 ;; 1 = read, 2 = write, 8 = append
                            0 ;; 0 = ascii, -1 = unicode, -2 system default
                        )
                    )      
                )
            )
            (vlax-invoke stream 'Write text)
            (vlax-invoke stream 'Close)
            (setq result (vlax-get file 'Size))
        )
    )

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

 

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

测试:(_writestream "d:\\jmj.sld" (vl-list->string (vl-string->list (binarytotext "d:\\bsph.sld"))) "w")

 

 

生产的JMJ.SLD的值和原来的BSPH.SLD的值是一样的,文件大小也相等!

但是预览效果也不同,为什么

bsph.SLD是这样的 

而JMJ.SLD是这样的

 

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2009-12-30 00:56 | 显示全部楼层
你所用的程序在实际使用时是有问题的,这有老外做的读写二进制文件工具,很好用;
加载后(注意07,08用ReadBin17.arx,05,06用ReadBin16.arx),用函数binget,binput即可。
以下为示例:
  1. ;读写幻灯片文件
  2. (defun c:test2 ( / )
  3. (setq a (binget "d:\\GB56.sld" ));读取二进制文件
  4. (binput "d:\\GB56-1.sld" a );写二进制文件
  5. )
  6. (defun c:test3 ( / )
  7. (binput "c:\\McCoy2.dwg" (binget "c:\\McCoy.dwg" ) )
  8. )

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复 支持 1 反对 0

使用道具 举报

发表于 2009-12-28 22:44 | 显示全部楼层

应该还有一个“textToBinary”

 楼主| 发表于 2009-12-28 23:09 | 显示全部楼层
楼上能留下QQ吗?想进一步请教
发表于 2009-12-29 10:20 | 显示全部楼层

我也是花了三天时间,硬是没有找到正确的解决方法。

问我也无解。

发表于 2010-7-6 11:42 | 显示全部楼层
非常好,多谢
发表于 2012-10-27 17:38 | 显示全部楼层
经试用,xianaihua 提供的方法的确好用!
发表于 2018-5-7 14:49 | 显示全部楼层
5楼的方法很好用,唯一的缺点就是.arx也没办法一直打包到vlx中,用在别的电脑上需先加载ReadBin17.arx

点评

yxp
办法有很多  发表于 2018-5-14 21:35
发表于 2021-10-4 22:38 | 显示全部楼层
学习一下怎么实现的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-5 06:50 , Processed in 0.279318 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表