Pack and unpack in Perl

Source: Internet
Author: User
Tags unpack



This pack, unpack in "Perl language programming" has introduced
It looks complicated.



1 # Convert a string to hex format
 2 my $source = ‘abcd‘;
 3 unpack(‘H*‘, $source);
 4 # Description, this ‘H’ is a description of conversion to hexadecimal, the following * is any length, that is, how much is it after conversion?
 5 #If you simply write unpack(‘H‘, $source), only the first character of the converted hexadecimal character is displayed.
 6
 7 # For example, if you want to convert a hexadecimal to ASCII, you can do this:
 8 # hexadecimal string
 9 $hex = "61626364";
10
11 #转ASCII
12 $bin = pack(‘H*‘, $hex);
13 print "$bin\n";
14
15
16 # This pack and unpack can extract data in addition to the hexadecimal, for example, have the following string:
17 my $data = ‘1234567890aABCDE,FG’;
18 #We want to raise 1234567890 and ABCDE FG, you can do this:
19 my($one, $two, $three) = unpack(‘A10xA5xa2‘, $data)
20 # A character here, it is represented by an A.
21 How many characters are there, just add a number after A, such as ‘ABC’, which is three characters, which means: A3
22 #如1234567890 has ten characters, it means A10
23 and the lowercase a, we don't want, it is represented by x, this x means skip, and it represents null in pack.
24 # Of course, if you want to extract all the bytes, you can add * after it, such as A* 


Transferred from: https://blog.gtwang.org/perl/perl-pack-unpack-tutorial/






Pack and unpack in Perl


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.