search
HomeBackend DevelopmentPHP ProblemHow to convert php files to hexadecimal

How to convert php files to hexadecimal

Dec 08, 2021 am 10:00 AM
phphexadecimal

How to convert php files to hexadecimal: 1. Create a PHP sample file; 2. Convert the file content to hexadecimal output through the "function fileToHex($file){...}" method That’s it.

How to convert php files to hexadecimal

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to convert php files to hexadecimal?

Example of method of converting files to hexadecimal in php

The code is as follows:

<?php
/**
 * php 文件与16进制相互转换
 * Date: 2017-01-14
 * Author: fdipzone
 * Ver: 1.0
 *
 * Func
 * fileToHex 文件转16进制
 * hexToFile 16进制转为文件
 */
 
/**
 * 将文件内容转为16进制输出
 * @param String $file 文件路径
 * @return String
 */
function fileToHex($file){
 if(file_exists($file)){
 $data = file_get_contents($file);
 return bin2hex($data);
 }
 return &#39;&#39;;
}
 
/**
 * 将16进制内容转为文件
 * @param String $hexstr 16进制内容
 * @param String $file 保存的文件路径
 */
function hexToFile($hexstr, $file){
 if($hexstr){
 $data = pack(&#39;H*&#39;, $hexstr);
 file_put_contents($file, $data, true);
 }
}
 
// 演示
$file = &#39;test.doc&#39;;
 
// 文件转16进制
$hexstr = fileToHex($file);
echo &#39;文件转16进制<br>&#39;;
echo $hexstr.&#39;<br><br>&#39;;
 
// 16进制转文件
$newfile = &#39;new.doc&#39;;
hexToFile($hexstr, $newfile);
 
echo &#39;16进制转文件<br>&#39;;
var_dump(file_exists($newfile));
 
?>

Output:

文件转16进制
efbbbf3130e4b8aae4bfafe58da7e69291e28094e280943235e4b8aae4bbb0e58da7e8b5b7...
 
16进制转文件
boolean true

Recommended study:《 PHP video tutorial

The above is the detailed content of How to convert php files to hexadecimal. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

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),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor