Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

native byte order integer and utf-8

by bhildred (Novice)
on Dec 08, 2013 at 02:26 UTC ( [id://1066176]=perlquestion: print w/replies, xml ) Need Help??

bhildred has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a test server to work with google chrome's native messaging. all messages are passed as a unsigned 32-bit number followed by that many bytes of utf-8 encodes json.

Ok so I read 4 bytes, and unpack my length, read length bytes and decode using utf8::upgrade and a json parser (tbd). And I take the response I generate, encode it as json determine length, and pack.

But the devil is in the details. perldoc -f pack is unclear as to the size of 'i'. Some places it appears this may vary by processor, and could be 32 or 64 bits. other places imply that it is 32 bits only. Also I am uncertain as to the best way to encode the response string should I use utf8::encode, pack, or something in Encode::

Your advice is appreciated.

Replies are listed 'Best First'.
Re: native byte order integer and utf-8
by Athanasius (Archbishop) on Dec 08, 2013 at 03:13 UTC

    Hello bhildred, and welcome to the Monastery!

    The sizes of the integer templates 'i' and 'I' are variable by design. For fixed-size integers, use one of following (signed/unsigned): c/C (8 bits), s/S (16 bits), l/L (32 bits), q/Q (64 bits). Since you specify an “unsigned 32-bit number”, use 'L':

    $packed = pack 'L', $count; ... $count = unpack 'L', $packed;

    (The template sizes are clearly documented on page 801 of the Camel Book, 4th Edition, 2012. L is “An unsigned long value, always 32 bits”.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: native byte order integer and utf-8
by BrowserUk (Patriarch) on Dec 08, 2013 at 07:52 UTC

    If you need unsigned 32-bit numbers of a particular endianess, use either 'V' for little-endian or 'N' for network-order (big-endian).

    Cuts out the guesswork and platform dependence of the other 32-bit(ish) templates.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I knew about V and N but the spec says native byte order, And since I didn't write the spec . . .

        but the spec says native byte order,

        Native to what? The producer? The consumer?

        What does the spec say about data transferred from little-endian to big-endian or vice versa?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1066176]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-24 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found