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

Hi monks,
I'm back for stupid questions!:)
Now, I'm studied perlpacktut,and confused by some usage of them.
1.
my $buf = pack( 'NA*', length( $msg ), $msg ); #sender my ($len_msg,$msg) = unpack('NA*',$buf);#receiver
why we need to add a 'pack/unpack' behavior while transfering data? in my opnion,I could transfer variable directly.
2.
$status = "11010101"; $ps = pack('b16',$stauts); print "$ps\n"; #junk char!!!!
Please tell me what there is in $ps.
3.
How do I convert decimal to hex,oct,bin or others? or I should use sprintf?

Thanks in advance!

Replies are listed 'Best First'.
Re: question of pack.
by planetscape (Chancellor) on Dec 26, 2006 at 08:05 UTC
Re: question of pack.
by gaal (Parson) on Dec 26, 2006 at 06:59 UTC
    If your two computer systems have different endianity, you have to arrange for them to treat the transmission on the wire as the same number. (Relatedly, if you ever use Storable, it's recommended to use nstore and not store, so the data works across hosts.)
Re: question of pack.
by thevoid (Scribe) on Dec 26, 2006 at 13:28 UTC
    re: converting decimal to hex and oct -
    use warnings; use strict; print "Please input a hex no. to convert to dec. :"; chomp (my $value = <STDIN>); print hex("$value"), "\n";
    Just replace hex( ) with oct( ) for oct. For binary there is no bin( ), I was struggling with the same problem recently -

    http://perlmonks.org/index.pl?node_id=591504

    should find some answers there. PS - can someone please tell me the tags to make urls clickable in a post? Ta

    Edit, sorry - just spotted that I got the conversion backwards in the code

      Hi thevoid,

      If you read the documentation, you'll see that oct is capable of conversion from binary.  It just requires putting a leading '0b' in front of the number.

      For example:

      use strict; use warnings; my $value = "0b101010"; printf "$value is %d decimal\n", oct($value);

      Not too long ago, I even wrote a submission about it, in which I admitted that I didn't know about this additional functionality of oct until much later in my Perl education.

      As for your other question, to make links clickable, you can do this:  [id://591504], which will appear like "Bitwise operators" in the rendered page.

      See Writeup Formatting Tips and Perl Monks Approved HTML tags for more information.

      Update:  Fixed a transcription typo.


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        Hi liverpole, thanks for the info.
Re: question of pack.
by ikegami (Patriarch) on Dec 28, 2006 at 03:24 UTC

    why we need to add a 'pack/unpack' behavior while transfering data?

    When the data is sent over a stream (e.g. a TCP socket), the reader has no way of knowing where the data ends.

    For example, if I told you "THE FOX IS BROWN", would you know if my sentence is done? If I continued with "AND FAST", is it part of this sentence, or is it the start of another?

    One solution is to use a terminal (the period of a sentence, for example) to mark the end of the data. Another solution is to prepend the data with its length (as in your code snippet).

    How do I convert decimal to hex,oct,bin or others? or I should use sprintf?

    For starters, the number is not stored in base 10. 255, 0xFF, 250+5 are all stored identically, so calling your number a "decimal" is wrong unless you mean it's not an integer.

    "Converting a number to hex/oct/bin" and "Formatting a number as hex/oct/bin" are short ways of saying "getting the string representation of a number in hex/oct/bin". sprintf is all about formatting numbers, so it's a very appropriate function to use. Other means can probably be used, but they won't be as readable.

Re: question of pack.
by xiaoyafeng (Deacon) on Dec 28, 2006 at 01:55 UTC
    Sorry for reply so late! I have spend two days in studying the artical as planetscape mentioned.:) According to that artical,pack is the funcation used for transfering data between machines.Number-convert is its side effect.We should use hex,oct,or sprintf to convert.
    print (unpack('B8',pack('H2','ff'))) #11111111 print (unpack('B8',pack('i','120'))) #01111000 print (unpack('H2',pack('i','15'))) #0f print (unpack('H',pack('i','15'))) #0
    As above,we can't get the answer you want unless we set correct digits.