Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

writing into binary file

1,264 views
Skip to first unread message

Sanjeev Kumar

unread,
Jul 9, 2009, 7:04:12 AM7/9/09
to sanj...@yahoo.com
I am pretty new to tcl/tk, i have written below code to create a
binary file with just one byte of data in it

set fp [open test.bin w]
fconfigure $fp -translation binary -encoding binary
puts -nonewline $fp [format binary s1 [expr 0x20]]
close $fp

when I open the file in binary editing mode (using UltraEdit), i am
seeing "20 00", it is appending 00 (NULL), don't know why...what else
needs to added to make it work properly

btw i am using tcl version 8.3.4

-Sanjeev

Les Cargill

unread,
Jul 9, 2009, 7:10:31 AM7/9/09
to

From http://tmml.sourceforge.net/doc/tcl/binary.html

"s
This form is the same as c except that it stores one or more 16-bit
integers in little-endian byte order in the output string. The low-order
16-bits of each integer are stored as a two-byte value at the cursor
position with the least significant byte stored first. For example,

binary format s3 {3 -3 258 1}

will return a string equivalent to \x03\x00\xfd\xff\x02\x01.
"

Emphasis "two-byte".

--
Les Cargill

Georgios Petasis

unread,
Jul 9, 2009, 7:15:00 AM7/9/09
to Sanjeev Kumar, sanj...@yahoo.com
O/H Sanjeev Kumar έγραψε:

Why do you need format? (Which does not work as written: it puts in the
file just "binary")

George

Sanjeev Kumar

unread,
Jul 9, 2009, 7:55:40 AM7/9/09
to
On Jul 9, 4:10 pm, Les Cargill <lcarg...@cfl.rr.com> wrote:
> Sanjeev Kumar wrote:
> > I am pretty new to tcl/tk, i have written below code to create a
> > binary file with just one byte of data in it
>
> > set fp [open test.bin w]
> > fconfigure $fp -translation binary -encoding binary
> > puts -nonewline $fp [format binary s1 [expr 0x20]]
> > close $fp
>
> > when I open the file in binary editing mode (using UltraEdit), i am
> > seeing "20 00", it is appending 00 (NULL), don't know why...what else
> > needs to added to make it work properly
>
> > btw i am using tcl version 8.3.4
>
> > -Sanjeev
>
>  Fromhttp://tmml.sourceforge.net/doc/tcl/binary.html

>
> "s
>      This form is the same as c except that it stores one or more 16-bit
> integers in little-endian byte order in the output string. The low-order
> 16-bits of each integer are stored as a two-byte value at the cursor
> position with the least significant byte stored first. For example,
>
>      binary format s3 {3 -3 258 1}
>
>      will return a string equivalent to \x03\x00\xfd\xff\x02\x01.
> "
>
> Emphasis "two-byte".
>
> --
> Les Cargill- Hide quoted text -
>
> - Show quoted text -

found the way replacing 's' with 'c' character works

set fp [open test.bin w]
fconfigure $fp -translation binary -encoding binary

puts -nonewline $fp [format binary c1 [expr 0x20]]
close $fp

Pat Thoyts

unread,
Jul 9, 2009, 9:51:41 AM7/9/09
to
Sanjeev Kumar <sanj...@gmail.com> writes:

>set fp [open test.bin w]
>fconfigure $fp -translation binary -encoding binary
>puts -nonewline $fp [format binary s1 [expr 0x20]]

I assume you mean [binary format s ..]

You asked it to write a 2 byte signed integer (aka short). Therefore
you got a file with two bytes in little-endian byte order.

If you want a single byte - use 'c'

--
Pat Thoyts http://www.patthoyts.tk/
To reply, rot13 the return address or read the X-Address header.
PGP fingerprint 2C 6E 98 07 2C 59 C8 97 10 CE 11 E6 04 E0 B9 DD

0 new messages