Null-terminated strings with Pack.write()

1 view
Skip to first unread message

Phil Rhodes

unread,
Dec 15, 2009, 9:58:30 PM12/15/09
to jsl...@googlegroups.com
Can we have a way to null-terminate strings written with Pack.write()?
 
Quite often, strings written to binary files are null-terminated, as in C, and it would be nice to have an option to do this.
 
P

Daniel Friesen

unread,
Dec 16, 2009, 2:39:10 AM12/16/09
to jsl...@googlegroups.com
Write a "\0", write a 1 byte 0 integer, or append a "\0" to the end of
your string. Writing a null byte is easy.
What's tougher is reading a null terminated string. Something that reads
until a binary sequence or EOF at a low level would be good.

~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]
> --
> Group jslibs - http://groups.google.com/group/jslibs -
> jsl...@googlegroups.com
> Unsubscribe: jslibs-un...@googlegroups.com

Phil Rhodes

unread,
Dec 16, 2009, 8:02:41 AM12/16/09
to jsl...@googlegroups.com
> Write a "\0", write a 1 byte 0 integer, or append a "\0" to the end of
> your string.

Wha, does JSlibs support a literal "\0" as a null in a string?

Actually, come to think of it, you could also write String.fromCharCode(0).
Could you? (type type type) yes you could.

> What's tougher is reading a null terminated string. Something that reads
> until a binary sequence or EOF at a low level would be good.

Seconded.

But readUntil() for files is more important. As is seekUntil().

P

Daniel Friesen

unread,
Dec 16, 2009, 12:40:31 PM12/16/09
to jsl...@googlegroups.com
Phil Rhodes wrote:
>> Write a "\0", write a 1 byte 0 integer, or append a "\0" to the end of
>> your string.
>>
>
> Wha, does JSlibs support a literal "\0" as a null in a string?
>
> Actually, come to think of it, you could also write String.fromCharCode(0).
> Could you? (type type type) yes you could.
> ...
> P
>
That's what ECMAScript supports, nothing to do with jslibs, \0 in a
string literal is a null char ("\0" and String.fromCharCode(0) output
the exact same thing).
Internally chars are stored in 16bits though jslibs only writes 8bits so
it's not a problem (well, besides the storage in memory being twice the
size of the data you are storing for any binary data stored in a
string). I still prefer having (and using) a set type for binary data.

soubok

unread,
Dec 16, 2009, 1:41:44 PM12/16/09
to jsl...@googlegroups.com
Yes, unfortunately there is nothing in JavaScript to store binary data
without having this twice-size issue.
jslibs address this issue with the Blob class that stores only 8bits
data and behaves like a JavaScript String. The Blob is seamlessly
mutated into a String when complex API (like regexp, ...) is used.

Franck.

Daniel Friesen

unread,
Dec 16, 2009, 2:26:51 PM12/16/09
to jsl...@googlegroups.com
For now. The CommonJS group is standardizing one now, and it's likely
that ECMA will take that and make it part of ECMAScript after some tweaks.
Reply all
Reply to author
Forward
0 new messages