Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pack, unpack and ord functions in php

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use pack, unpack and ord functions in php. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

In my work, I have also come to understand the power of pack,unpack,ord for binary byte handling. Let me introduce them one by one. It is estimated that not many of them are used in our work. In my recent work, because the communication needs to use the binary stream, and then the interface is received by php. At that time, during the processing, I consulted a lot of information. Because they are rarely used, and few friends use them at work. In my work, I have also come to understand the power of pack,unpack,ord for binary byte handling. Let me introduce them one by one.

Pack function description: this function is used to compress and package data into a string.

Syntax: pack (format,args+)

Parameter description

Format is required. Specifies the format to be used when wrapping data.

Args+ is optional. Specify one or more parameters to be packaged.

Character description

A fill the string blanks with NULL characters

A fill the string white space with SPACE characters (spaces)

H hexadecimal string, low first

H hexadecimal string, high order comes first

C signed character

C unsigned character

S signed short integer (sixteen bits, according to the bit order of the computer)

S unsigned short integer (sixteen bits, according to the bit order of the computer)

N unsigned short integers (sixteen bits, followed by high bits)

V unsigned short integers (sixteen bits, order after low bits)

I signed integer (according to the order and range of the computer)

I unsigned integers (in the order and range of the computer)

L signed long integers (32 digits, in the order of the bits of the computer)

L unsigned long integers (32 bits, according to the order of the bits of the computer)

N unsigned short integers (32 bits, followed by high bits)

V unsigned short integer (32 digits, the order in which the low order comes later)

F single exact floating point number (depending on the range of the computer)

D times the exact floating point number (depending on the range of the computer)

X vacancy

X goes back to one.

@ fill in NULL characters to absolute position

Unpack function description: this function is used to decompress the data of the bit string

Syntax: unpack (format,args+)

Parameter description

Format is required. Specifies the format to be used when wrapping data.

Args+ is optional. Specify one or more parameters to be packaged.

The parameters are the same as pack.

Ord function description: returns the acill code value of the corresponding character

Syntax: ord ($character)

An example shows:

The copy code is as follows:

From the above example, we can see that the same string is stored in different formats with different bytes. You can also see here that saving space can be achieved by saving characters in different formats. And enable the unreadable encryption effect. It suddenly occurred to me that designing a database field type is a problem if a field is just a 10-bit length integer. We set it to shaping: 256 "256" 256 is 4 bytes, if set to 10 length strings. That's 10 bytes. The whole digestion space is twice as large. Setting the correct character type is of great help to improve database performance. Oh, it's a little beside the point.

An example Analysis of Bytecode Communication with php

The pack function I just mentioned: saving space and encrypting format

Here are two examples to illustrate the requirements of interface development:

Parameter description

User name 20 bytes, character type

Password 10 bytes, character type

Age 1 byte, uncharted type

Date of birth 4 bytes, integer (19800101)

Mailbox 50 bytes, string

Use "\ 0" to split between fields

A, PACK packet

The copy code is as follows:

Because it is divided by "\ 0", the total length is 89 bytes. Through the above output, some string outputs can be read, others have become garbled. That's why I said it could be kept secret.

B, Unpack unpacking

Unpacking needs to be read according to the packaging method, how long the reading is and what type of reading should be used, which must be the same as the packaging rules.

The copy code is as follows:

These are all the contents of this article entitled "how to use pack, unpack and ord functions in php". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report