$rc = unpack('d',pack('CCCCCCCC',@array));
@array is an array with 8 numbers, for example:
"119 190 159 26 239 220 145 64"
$rc would be 1143.2335 in this case. I started by making two
statements out of the original Perl code:
$rc1 = pack('CCCCCCCC',@array);
$rc = unpack('d',$rc1);
The first line translates to
foreach item $array { append rc1 [binary format c $item] }
but this does not give the same result in Tcl and Perl. I'm stuck. Can
someone give me a hint?
Thank you, Torsten
This worked for me:
set l [list 119 190 159 26 239 220 145 64]
set rc [eval binary format cccccccc [join $l]]
binary scan $rc d result
puts $result
This will depend on the endianness of your platform, but try:
set packed [binary format [string repeat c 8] 119 190 159 26 239 220 145
64]
binary scan $packed d dbl
puts $dbl
Regards,
Arjen
Great! Thank you very much!
Torsten
:This worked for me:
:
:set l [list 119 190 159 26 239 220 145 64]
:set rc [eval binary format cccccccc [join $l]]
:binary scan $rc d result
:
:puts $result
When I run these tcl lines in Tcl 8.4.2, I get
6.31920840155e+268
which doesn't seem like it would be equal to 1143.2335 .
--
Join us at the Tenth Annual Tcl/Tk Conference <URL: http://mini.net/tcl/6274 >
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.
<URL: mailto:lvi...@yahoo.com > <URL: http://www.purl.org/NET/lvirden/ >
I guess it depends on the endedness of the platform. I get the same
result on solaris, but reversing the list:
proc lreverse {list} {
set tsil [list]
foreach element $list {
set tsil [linsert $tsil 0 $element]
}
return $tsil
}
set l [lreverse [list 119 190 159 26 239 220 145 64]]
set rc [eval binary format cccccccc $l]
binary scan $rc d result
puts $result
gives the expected result.
I suppose with the recent K discussion:
proc K {x y} {set x}
proc lreverse {list} {
set tsil {}
foreach element $list {
set tsil [linsert [K $tsil [set tsil {}]] 0 $element]
}
return $tsil
}
(and I note the inconsistent use of explicit/implicit returns... What a topical
posting ;)
--
Glenn Jackman
NCF Sysadmin
gle...@ncf.ca
Then you are using a machine with BigEndian instead of
LittleEndian.
Remind me to write a TIP on this limitation of the [binary]
command.
Regards,
Arjen
Ah, this is quite equivalent to the method proposed by Steve Howarth.
But hey, this is cool, because the endianness dependance wasn't clear
to me. One line up in the Perl I'm translating, it does just what
Glenn Jackman mentioned: it reverses the given list (claiming to be on
an x86 platform). So now, I can even translate the code AND be sure it
is independant of the used platform.
Hmm, perhaps another question. What I'm really trying to do is
extracting data from a Paradox database file. So I searched the net
for any knowledge about the file format and found a Perl module and a
descriptive text (that's where the code above comes from). Since all
this information is inofficial and the file format is not published by
Borland/Corel, I wonder whether it would be legal to build a Tcl
package and give it to the community. In other words: is
re-engineering allowed?
Torsten
Some people are using TclODBC with Paradox.
No regulars here are lawyers. The little I understand
about intellectual property law includes that it apparently
could be a full-time job just researching the question of
how legal it is to reverse engineer various constructs.
At an informal level, I've seen secondary sources that
claim rights to reverse engineer are recognized in the
laws of some European countries, I can attest that a LOT
of it goes on, and that, if I were working currently with
Paradox, the risk of legal action by Borland would be low
on my personal list of worries.
Remember, though: in the US, innocence is no particular
defense against indictment.
--
Cameron Laird <Cam...@Lairds.com>
Business: http://www.Phaseit.net
Personal: http://phaseit.net/claird/home.html
It depends on the license of the Perl module. If the Perl module does
not prevent you from reengineering the Perl module, then I think you
can reengineer it safely.
On the other hand, if the Perl module author broke the law in his creating
of the module, then you would be continuing that action by re-re-engineering
it...
Your [lreverse] procedure is better implemented as:
proc lreverse { list } {
set newlist {}
set i [llength $list]
for { incr i -1 } { $i >= 0 } { incr i -1 } {
lappend newlist [lindex $list $i]
}
return $newlist
}
Trust me on this. I use this very example in my classes. :-)
- Ken Jones, President
Avia Training and Consulting
www.avia-training.com
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
Not that I don't trust you implicitly, but... ;)
What's the difference? (apart from style)
Speed, the 'set newlist [linsert $newlist 0 $element]' method requires an
internal copy of the built up list with each iteration (and rebuffering as
the list grows), whereas the lappend does not. You should see at least a
300X speedup using the latter method.
Tom Wilkason
proc lreverse L {
set res {}
set i [llength $L]
while {$i} {lappend res [lindex $L [incr i -1]]} ;# rmax
set res
} ;# RS, tuned 10% faster by [rmax]
Michael Schlenker
YMMV
Tom Wilkason