❮ Tham khảo Linh tinh PHP
Thí dụ
Giải nén dữ liệu từ một chuỗi nhị phân
$data = "PHP";
print_r[unpack["C*",$data]];
?>
Định nghĩa và cách sử dụng
Hàm unpack[] giải nén dữ liệu từ một chuỗi nhị phân
cú pháp
Giá trị tham số
Tham sốMô tảĐịnh dạngBắt buộc. Chỉ định định dạng sẽ sử dụng khi giải nén dữ liệuNhững giá trị khả thi
- a - Chuỗi đệm NUL
- A - Chuỗi đệm SPACE
- h - Chuỗi lục giác, mức thấp nhất trước
- H - Chuỗi lục giác, cao nibble đầu tiên
- c - ký tự
- C - ký tự không dấu
- s - ký ngắn [luôn là 16 bit, thứ tự byte máy]
- S - unsigned short [luôn là 16 bit, thứ tự byte máy]
- n - unsigned short [luôn luôn là 16 bit, thứ tự byte cuối lớn]
- v - unsigned short [luôn luôn là 16 bit, thứ tự byte cuối nhỏ]
- i - số nguyên đã ký [kích thước phụ thuộc vào máy và thứ tự byte]
- I - số nguyên không dấu [kích thước phụ thuộc vào máy và thứ tự byte]
- l - ký dài [luôn là 32 bit, thứ tự byte máy]
- L - dài không dấu [luôn là 32 bit, thứ tự byte máy]
- N - dài không dấu [luôn là 32 bit, thứ tự byte cuối lớn]
- V - dài không dấu [luôn là 32 bit, thứ tự byte cuối nhỏ]
- q - long long đã ký [luôn là 64 bit, thứ tự byte máy]
- Q - long long không dấu [luôn là 64 bit, thứ tự byte máy]
- J - dài không dấu [luôn luôn là 64 bit, thứ tự byte cuối lớn]
- P - dài không dấu dài [luôn luôn là 64 bit, thứ tự byte cuối nhỏ]
- f - float [kích thước và biểu diễn phụ thuộc vào máy]
- g - float [kích thước phụ thuộc vào máy, thứ tự byte cuối nhỏ]
- G - float [kích thước phụ thuộc vào máy, thứ tự byte cuối lớn]
- d - double [kích thước và biểu diễn phụ thuộc vào máy]
- e - double [kích thước phụ thuộc vào máy, thứ tự byte cuối nhỏ]
- E - double [kích thước phụ thuộc vào máy, thứ tự byte cuối lớn]
- x - NUL byte
- X - Sao lưu một byte
- Z - Chuỗi đệm NUL
- @ - điền NUL vào tuyệt đối
chi tiết kỹ thuật
Giá trị trả về. Trả về một mảng nếu thành công, hoặc FALSE nếu thất bại. Phiên bản PHP. 4+Nhật ký thay đổi. PHP7. 2 - float và double hiện hỗ trợ cả endian lớn và nhỏ.PHP 7. 1 - Đã thêm tham số offset tùy chọn.
PHP 5. 5. 0 - Các thay đổi sau được thực hiện để tương thích với Perl. Mã "a" hiện giữ lại các byte NULL ở cuối.
Mã "A" hiện loại bỏ tất cả các khoảng trắng ASCII ở cuối.
Mã "Z" đã được thêm vào cho các chuỗi đệm NULL và loại bỏ các byte NULL ở cuối.
Thêm ví dụ
Thí dụ
Giải nén dữ liệu
$data = "PHP";
print_r[unpack["C*myint",$data]];
?>
Thí dụ
Giải nén dữ liệu
$bin = pack["c2n2",0x1234,0x5678,65,66];
print_r[unpack["c2chars/n2int",$bin]];
?>
Như chúng ta biết bất cứ khi nào ghi lên tệp, phương thức write[] của lớp Tệp sẽ phát huy tác dụng nhưng ở đây chúng ta không thể sử dụng nó để chuyển đổi byte đó thành tệp. Để chuyển đổi một mảng byte thành một tệp, chúng ta sẽ sử dụng một phương thức có tên là phương thức getBytes[] của lớp String
Thực hiện. Chuyển đổi một Chuỗi thành một mảng byte và ghi nó vào một tệp
Thí dụ
Java
Successfully byte inserted8
Successfully byte inserted9
Successfully byte inserted0
Successfully byte inserted1
Successfully byte inserted0
Successfully byte inserted3
Successfully byte inserted0
Successfully byte inserted5
Successfully byte inserted0
Successfully byte inserted1
Successfully byte inserted2
Successfully byte inserted3
Successfully byte inserted4
Successfully byte inserted5
Successfully byte inserted4
Successfully byte inserted7
Successfully byte inserted8
Successfully byte inserted9
Successfully byte inserted80
Successfully byte inserted4
Successfully byte inserted7
Successfully byte inserted83
Successfully byte inserted84
Successfully byte inserted85
Successfully byte inserted4
Successfully byte inserted87
Successfully byte inserted4
Successfully byte inserted89
Successfully byte inserted4
Successfully byte inserted7
Successfully byte inserted92
Successfully byte inserted93
Successfully byte inserted94
Successfully byte inserted95
Successfully byte inserted4
Successfully byte inserted97
Successfully byte inserted98
Successfully byte inserted99
Successfully byte inserted98
Successfully byte inserted01
Successfully byte inserted97
Successfully byte inserted03
Successfully byte inserted04
Successfully byte inserted03
Successfully byte inserted06
Successfully byte inserted03
Successfully byte inserted08
Successfully byte inserted84
Successfully byte inserted10
Successfully byte inserted03
Successfully byte inserted12
Successfully byte inserted03
Successfully byte inserted14
Successfully byte inserted03
Successfully byte inserted16
Successfully byte inserted03
Successfully byte inserted18
Successfully byte inserted03
Successfully byte inserted00
Successfully byte inserted01
Successfully byte inserted02
Successfully byte inserted03
Successfully byte inserted04
Successfully byte inserted05
Successfully byte inserted03
Successfully byte inserted07
Successfully byte inserted03
Successfully byte inserted09
Successfully byte inserted98
Successfully byte inserted31
Successfully byte inserted98
Successfully byte inserted33
Successfully byte inserted98
Successfully byte inserted35
Successfully byte inserted36
Successfully byte inserted03
Successfully byte inserted38
Successfully byte inserted03
Successfully byte inserted00
Successfully byte inserted01
Successfully byte inserted02
Successfully byte inserted98
Successfully byte inserted31
Successfully byte inserted4
Successfully byte inserted31
Successfully byte inserted4
Successfully byte inserted08
_______04____450
Successfully byte inserted4
Successfully byte inserted1
Successfully byte inserted7
Successfully byte inserted92
Successfully byte inserted55
Successfully byte inserted4
Successfully byte inserted97
Successfully byte inserted98
Successfully byte inserted59
Successfully byte inserted98
Successfully byte inserted01
Successfully byte inserted02
_______003____403
Successfully byte inserted05
_______003
Successfully byte inserted03____008
Successfully byte inserted80
Successfully byte inserted98
Successfully byte inserted11
Successfully byte inserted98
Successfully byte inserted13
Successfully byte inserted98
Successfully byte inserted94
Successfully byte inserted16
Successfully byte inserted98
Successfully byte inserted18
Successfully byte inserted98
Successfully byte inserted20
Successfully byte inserted98
Successfully byte inserted22
Successfully byte inserted4
Successfully byte inserted31
Successfully byte inserted31
đầu ra. Trên bảng điều khiển
Successfully byte inserted7
Bây giờ chúng ta hãy thảo luận về trường hợp sử dụng để viết các Giá trị Số nguyên, Nhân đôi, Ký tự trong Tệp [sử dụng Lớp Trình bao bọc]