Home php教程 php手册 用php操作二进制文件(有二进制操作经验的人进来给偶解答一下下吧,谢谢丫)

用php操作二进制文件(有二进制操作经验的人进来给偶解答一下下吧,谢谢丫)

Jun 06, 2016 pm 02:06 PM
php binary code Open source document experience programming programming language software development

二进制对于php来说是一个很偏的东西,因为php中对于二进制的支持是非常有限的,今天在一个php群组里讨论一个问题讨论了一下午也没讨论出结果来,现在总结一下,不知道我想的对不对,因为本来还觉得自己挺理解的,后来讨论多了,连二进制是什么都糊涂了.

问题:群里有个朋友接受一个项目,以前是用java写的,里面有很多对二进制文件的操作,包括图片作为二进制存储,然后在这个二进制文件中加入一些用户输入的字符串的二进制格式,就是把图片和字符串组合到一个二进制文件中,我们卡在了怎么把字符串写入到二进制文件中的问题这里.

 1 php
 2 $header_str="abc123";
 3 
 4 function asc2bin ($temp) { 
 5   $len = strlen($temp); 
 6   for ($i=0$i$len$i++$data.=sprintf("%02x",ord(substr($temp,$i,1))); 
 7   return $data
 8 }
 9 
10 $aa = asc2bin($header_str);
11 
12 
13 $content_2=pack('H*', $aa);
14 
15 $handle = fopen ("aaaa.bin", "wb");
16 fwrite($handle, $content_2);
17 fclose($handle);
18 
19 ?>

下面是我的最终代码,它将一个abc123的字符串作为二进制流写进二进制文件 aaaa.bin中,里面用到了pack函数,这个函数可以将php的内置类型转换成二进制格式,它和unpack配套,后者可以讲二进制转换成php的内置类型.

二者的用法如下:

   用php操作二进制文件(有二进制操作经验的人进来给偶解答一下下吧,谢谢丫)   用php操作二进制文件(有二进制操作经验的人进来给偶解答一下下吧,谢谢丫)Code

上面是用法,但是在我们的讨论中遇到了问题,归根结底就是说 二进制文件有没有可能在记事本里显示成不是乱码的形式呢?群上很多人说二进制文件在记事本里查看就应该是乱码,我一开始也深信不移,所以对自己的程序也没有了信心,因为我这个二进制文件虽然在高级文本处理工具里查看,十六进制形式的确是正确的,可是在记事本中竟然不会显示乱码,而是显示"abc123",我开始想我应该搞错了,我应该把字符串直接写进二进制文件中了,而不是写的二进制流.

现在我回来想了想,突然想到二进制文件就一定是乱码了么?英文字母就是以字节编码的,一个英文字母占一字节,8位,我把一个字符串的二进制写入一个二进制文件后,在记事本中当然会显示文本啦,因为记事本自动解析二进制,然后转换成字符串的格式了,我觉得我上面的程序其实是正确的.

其实我也不敢确认,二进制文件是个很复杂的概念,不同的二进制文件有着不同的结构,对于二进制的了解太少了,群上都没有一个能说清楚二进制文件到底是什么,怎么向二进制文件写入字符串就更别提了.

这里搞.NET的人多,大家应该都对二进制有概念吧,急问高手,我这样写二进制文件对还是不对啊,不问代码,而是字符串的二进制形式写到文件后,显示的是乱码还是字符串本身啊,望高手解答.

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Demystifying C: A Clear and Simple Path for New Programmers Demystifying C: A Clear and Simple Path for New Programmers Oct 11, 2024 pm 10:47 PM

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

See all articles