Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pack and byte length.

Reply
Thread Tools

pack and byte length.

 
 
Fabrice
Guest
Posts: n/a
 
      09-18-2008
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

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      09-18-2008

Quoth Fabrice <(E-Mail Removed)>:
>
> 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

--
Many users now operate their own computers day in and day out on various
applications without ever writing a program. Indeed, many of these users
cannot write new programs for their machines...
-- F.P. Brooks, 'No Silver Bullet', 1987 [(E-Mail Removed)]
 
Reply With Quote
 
 
 
 
Fabrice
Guest
Posts: n/a
 
      09-18-2008
On Sep 17, 5:49*pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth Fabrice <(E-Mail Removed)>:
>
>
>
> > 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


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".






 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      09-18-2008

Quoth Fabrice <(E-Mail Removed)>:
> On Sep 17, 5:49*pm, Ben Morrow <(E-Mail Removed)> wrote:
> > Quoth Fabrice <(E-Mail Removed)>:
> >
> > > 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.

>
> 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

--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
http://www.velocityreviews.com/forums/(E-Mail Removed) Frank Herbert, 'Dune'
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      09-18-2008
Fabrice <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excellent discount software packs - ImTOO Ripper Pack Platinum and ImTOO Ripper Pack Gold zhangelf01@gmail.com DVD Video 6 09-17-2006 03:27 AM
How to extract x amount of bytes from a byte object and store into another byte obj. DaBeef Java 1 07-21-2006 05:20 PM
read/write data byte-per-byte to and from a socket crash.test.dummy Java 1 02-17-2006 06:18 AM
How to byte[ ] --> char[ ] and char[ ] --> byte[ ]? Richard Java 11 02-01-2006 07:34 AM
Poor Mans NIMH Battery Pack and SLA Battery Pack ajacobs2 Digital Photography 0 08-19-2003 12:42 PM



Advertisments