_ReadStream
(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
(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
(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
(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 ...