Chas. Owens : > On Dec 8, 2007 8:32 AM, Niu Kun <[EMAIL PROTECTED]> wrote: >> Dear all, >> I'm new here. >> I've got a number larger than 128. >> I want to send it by socket. >> I find that if print the number directly, I'll print the number in ascii >> format. >> So I'll have 3 separate numbers transmitted. >> But I only want to transmit one byte. >> How can I do so? >> >> Thanks for any help. > > I believe you are looking for the pack* function. It will let you > take a list Perl scalars and convert them into the specified > representation. You might also be looking for the chr** function > which will return the appropriate character; however, since Perl now > uses UTF-8 the chr is no longer guaranteed to produce a value that > takes up only one byte. For instance, chr(0x263a), WHITE SMILING > FACE, returns three bytes (E2, 98, and BA). The opposite functions > are unpack*** and ord****. Also note that the largest value one byte > can hold is 255 (if it is unsigned, 127 if it is signed). > > Off hand I would say you want something like > > die "can't fit $val in an unsigned byte" if $val < 0 or $val > 255; > print $sock pack "C", $val; > > on the sending end and > > sysread $sock, $buf, 1; > my $val = unpack "C", $buf; > > on the receiving end; however, it a good network protocol will include > a lot more information than this. Perhaps if you explain what you are > trying to accomplish we can point out a higher level interface that > will serve your needs better (handling raw sockets is generally a sign > you are reinventing the wheel). > > * see "perldoc -f pack" or http://perldoc.perl.org/functions/pack.html > ** see "perldoc -f chr" or http://perldoc.perl.org/functions/chr.html > *** see "perldoc -f unpack" or http://perldoc.perl.org/functions/unpack.html > **** see "perldoc -f ord" or http://perldoc.perl.org/functions/ord.html Thank your for your remindness. I'll try the pack one.:-)
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/