php实现二进制和文本相互转换的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php实现二进制和文本相互转换的方法。分享给大家供大家参考。具体如下:

这段代码包含两个函数,bin2text,二进制转换为文本,text2bin,文本转换成二进制

<?php
function bin2text($bin_str)
{
 $text_str = '';
 $chars = explode("\n",chunk_split(str_replace("\n",'',$bin_str),8));
 $_I = count($chars);
 for($i = 0; $i < $_I; $text_str .= chr(bindec($chars[$i])), $i );
 return $text_str;
}
function text2bin($txt_str)
{
 $len = strlen($txt_str);
 $bin = '';
 for($i = 0; $i < $len; $i )
 {
  $bin .= strlen(decbin(ord($txt_str[$i])))<8?str_pad(decbin(ord($txt_str[$i])),8,0,STR_PAD_LEFT):decbin(ord($txt_str[$i]));
 }
 return $bin;
}
print text2bin('How are you gentlements?');
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

php 判断访客是否为搜索引擎蜘蛛的函数代码

复制代码 代码如下: /** * 判断是否为搜索引擎蜘蛛 * * @author Eddy * @return bool */ function isCrawler() { $agent...

php 无极分类(递归)实现代码

复制代码 代码如下:<?php $conn=mysql_connect("localhost","root","myoa888"); mysql_select_db("lost63...

php根据一个给定范围和步进生成数组的方法

这里给定开始和结束值,再给定一个步进值,就可以生成一个等差数组。 function array_range($from, $to, $step=1){ $array = arra...

PHP读取txt文件的内容并赋值给数组的代码

2010-12-15.txt的文件内容如下: 复制代码 代码如下: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20...

smarty section简介与用法分析

基本原形为: {section name = name loop = $varName[, start = $start, step = $step, max = $max, show...