php中,对数据类型的转换可直接使用pack来完成,比如表示字符型,短整型、整型、长整型可分别使用参数c、s、i、N来表示,当然这里转换后的数据为二进制数据,是不可读的,为了能正常读取,可直接unpack来将数据从二进制流还愿成可读的字符类型。
以下为各各类型的实际长度。
$uid = 1346563572;
长度为10,直接为字符类型来计算
echo strlen($uid); // 10
转换短整型,长度为2,当然这里超过了短整型的值范围,所以高位数据会丢失
echo strlen(pack(‘s’,$uid)); // 2
转换为整型,长度为4
echo strlen(pack(‘i’,$uid)); // 4
转换成长整型,长度为4
echo strlen(pack(‘N’,$uid)); //4
其它类型都正确,唯独长整型在64bit机器上始终表示4位,应该为8bit才对,所以为了解决这种问题,在不丢失数据的数据,可采用以下方式进行转换。
$i = 333333333333333333;
$v = pack(“NN”,$i >> 32,$i&0xFFFFFFFF);
file_put_contents(“/tmp/long.txt”,$v);
echo strlen($v)
长度为8
读取可采用以下方式
$v = file_get_contents(“/tmp/long.txt”);
list ($hi,$lo) = array_values(unpack(“N*N*”,$v));
if ($hi if ($ho echo ($hi
备注:以上脚本均在64位版本的php环境中运行。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function