Well, you needn't write any C code although it would probably run
faster than the following Tcl/Scotty code:
# Reads at most maxBytes bytes from the file specified by fileID and
# returns the scotty's hex representation of the bytes. If the maxBytes
# argument is not given, then all the remaining bytes in the file are
# read. The file may contain 0-bytes.
#
proc read_bin_return_hex {fileID {maxBytes -1}} {
set hex ""
set byte [read $fileID 1]
if {"$byte" != ""} {
append hex [mib scan sysDescr $byte]
} else {
# Something empty has been read -- it's an EOF, or a 0-byte.
#
if [eof $fileID] {
return $hex
}
append hex 00
}
if {$maxBytes != -1} {
incr maxBytes -1
}
set bin ""
while {$maxBytes != 0} {
set byte [read $fileID 1]
if {"$byte" != ""} {
append bin $byte
} else {
if [string length $bin] {
append hex ":[mib scan sysDescr $bin]"
set bin ""
}
if [eof $fileID] {
break
}
append hex ":00"
}
if {$maxBytes != -1} {
incr maxBytes -1
}
}
if [string length $bin] {
append hex ":[mib scan sysDescr $bin]"
}
return $hex
}
I've also checked some Tcl F.A.Q.s for the problem of the null bytes,
and have found the following:
What: binary I/O (LoVerso)
Where: <URL:ftp://ftp.aud.alcatel.com/tcl/code/binary-io-hack.shar.gz>
Description: Tcl based way of reading binary data containing nulls.
Contact: <URL:mailto:John@LoVerso.Southborough.MA.US> (John Robert LoVerso)
What: binary I/O (Moss)
Where: <URL:ftp://ftp.aud.alcatel.com/tcl/extensions/BinarIO.tar.gz>
Description: Package for performing unstructured binary I/O in Tcl.
Contact: <URL:mailto:joe@italia.rain.com> (Joseph V. Moss)
Archive-name: tcl-faq/usage
~From: Tcl Q&A
~Subject: Q.B13- How can I input and output binary data?
Internally, the Tcl interpreter stores nearly everything in
null-terminated strings. This procludes the possibility of directly
storing binary data (or more specifically, anything with embedded
nulls).
However, certain operations can be performed without the data being
stored in Tcl variables. For example, file handles can be attached
directly to external programs, like this:
set infp [open "|compress -dc $fileName"]
exec gzip -c $newFileName <@ $infp
or in Extended Tcl:
set infp [open "|compress -dc $fileName"]
set outfp [open "|gzip -c $newFileName" w]
copyfile $infp $outfp
Alternatively, you can in some way convert the data to a form that is
usable from Tcl:
* Reading from or writing to a pipe that is connected to an external
filter, such as atob/btoa, uuencode/uudecode, or even od.
* A method of handling binary data one character at a time using
standard Tcl can be found in:
ftp.aud.alcatel.com:/tcl/code/binary-io-hack.shar.gz
* There are also two different extensions available that enable the
handling of binary data: tclbin and BinarIO. The former is better
for handling structured data, while the latter works better for
binary data streams.
Thanks to Wayne Throop throop@aur.alcatel.com for his contribution to
this section.
Hope this helps.
v
-- rpr. : Robert B. Premuz
Internet: rpremuz@malik.srce.hr * Voice at home: +385 (0)1 687564