pack and byte length.

Discussion in 'Perl Misc' started by Fabrice, Sep 18, 2008.

  1. Fabrice

    Fabrice Guest

    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
     
    Fabrice, Sep 18, 2008
    #1
    1. Advertising

  2. Fabrice

    Ben Morrow Guest

    Quoth Fabrice <>:
    >
    > 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 []
     
    Ben Morrow, Sep 18, 2008
    #2
    1. Advertising

  3. Fabrice

    Fabrice Guest

    On Sep 17, 5:49 pm, Ben Morrow <> wrote:
    > Quoth Fabrice <>:
    >
    >
    >
    > > 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".
     
    Fabrice, Sep 18, 2008
    #3
  4. Fabrice

    Ben Morrow Guest

    Quoth Fabrice <>:
    > On Sep 17, 5:49 pm, Ben Morrow <> wrote:
    > > Quoth Fabrice <>:
    > >
    > > > 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.
    Frank Herbert, 'Dune'
     
    Ben Morrow, Sep 18, 2008
    #4
  5. Fabrice

    Guest

    Fabrice <> 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.
     
    , Sep 18, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Jones
    Replies:
    0
    Views:
    381
    Tim Jones
    Jan 31, 2004
  2. pack a three byte int

    , Nov 8, 2006, in forum: Python
    Replies:
    18
    Views:
    1,127
    Gabriel Genellina
    Nov 10, 2006
  3. Aaron Fude
    Replies:
    2
    Views:
    904
    Daniel Pitts
    Dec 17, 2008
  4. Shaun Jackman

    pack V length changes

    Shaun Jackman, Aug 12, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    74
    Bob Walton
    Aug 12, 2003
  5. Alexander Farber

    pack 'C3U*' not same as pack 'C3(xC)*'

    Alexander Farber, Jun 23, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    137
    Ilmari Karonen
    Jun 23, 2005
Loading...

Share This Page