Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

string, or integer?

by Ryszard (Priest)
on May 10, 2004 at 14:57 UTC ( #352089=perlquestion: print w/ replies, xml ) Need Help??
Ryszard has asked for the wisdom of the Perl Monks concerning the following question:

Its the end of a very long day, and i'm a bit stuck.

I need to send two bytes to a daemon to let it know the message length, the bytes have to bytes and not an ascii representation of bytes.

For example, I want 00001001, not 00111001.

unpack("B*", int(length($msg)/256)) yeilds the latter, not the former, how do I force the case?

sigh.

Replies are listed 'Best First'.
Re: string, or integer?
by halley (Prior) on May 10, 2004 at 15:02 UTC
    You should have guessed that pack() is the opposite of unpack(). From perldoc -f pack:
      The resulting string is the concatenation of the converted values. Typically, each converted value looks like its machine-level representation.
    my $bytes = pack("n", 9); # network byte order for 0x0009

    --
    [ e d @ h a l l e y . c c ]

Re: string, or integer?
by dave_the_m (Prior) on May 10, 2004 at 15:05 UTC
    pack 'n', length($msg)
    (Replace n with N or S or whatever depending on what order (if any) you need the two bytes in

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://352089]
Approved by Corion
Front-paged by been42
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2016-07-02 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (18 votes). Check out past polls.