pack and byte length.

F

Fabrice

Hi,

Whats the proper way to figure out the length , in bytes, of the
result of a pack command ?

That is: I use the pack command then write the result in a file, I
want to know the actual number of bytes written ?

Thanks
 
B

Ben Morrow

Quoth Fabrice said:
Whats the proper way to figure out the length , in bytes, of the
result of a pack command ?

That is: I use the pack command then write the result in a file, I
want to know the actual number of bytes written ?

Err, perldoc -f length? If that isn't what you want, you will need to
post your code.

Ben
 
F

Fabrice

Quoth Fabrice <[email protected]>:





Err, perldoc -f length? If that isn't what you want, you will need to
post your code.

Ben

I did read the length doc, and its not that clear.

By default length return the character length not the byte length. The
doc mention bytes::length but also says: "Note that the internal
encoding is variable, and the number of bytes usually meaningless."

It is not clear if the internal encoding and the result of "print" to
a file will yield the same number of bytes in all cases (eg: windows
vs unix platform, binary vs text mode files etc...)

So yes, length (or rather bytes::length) works, but I'm wondering if
this is the "proper way".
 
B

Ben Morrow

Quoth Fabrice said:
I did read the length doc, and its not that clear.

By default length return the character length not the byte length. The
doc mention bytes::length but also says: "Note that the internal
encoding is variable, and the number of bytes usually meaningless."

Ah, I see, and I appreciate your confusion: Perl's behaviour in this
area is complicated, and not necessarily easy to understand.

pack always returns a byte string, not a character string; that is,
length and bytes::length will always give the same result, and this will
be the actual number of bytes printed.
It is not clear if the internal encoding and the result of "print" to
a file will yield the same number of bytes in all cases (eg: windows
vs unix platform, binary vs text mode files etc...)

If you are writing binary data to a file you must make sure to call
binmode on the filehandle (or open it with the :raw layer). If you have
then the same number of bytes will be printed in every case; if you try
to print a character string with characters that don't fit into a byte,
you will get a 'Wide character in print' warning. pack will never return
such a string unless you use the 'U' template.
So yes, length (or rather bytes::length) works, but I'm wondering if
this is the "proper way".

Yes, it is.

Ben
 
X

xhoster

Fabrice said:
Hi,

Whats the proper way to figure out the length , in bytes, of the
result of a pack command ?

What are you packing?

perl -le 'print "length of float is ", length pack "f",0;'

If you are packing unicode, things might be different.

That is: I use the pack command then write the result in a file, I
want to know the actual number of bytes written ?

If you use the "syswrite" to write to disk, it will tell you how many bytes
it wrote. Or you can do a "tell" before and after and do the math.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top