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

yipeiwu_com5年前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 无限级分类,超级简单的无限级分类,支持输出树状图

无平台限制 只需要告知id,parentid,name 即可 <?php error_reporting(E_ALL ^ E_NOTICE); class Tree...

PHP错误提示It is not safe to rely on the system……的解决方法

在php程序开发中有时会出现类似于这样的警告: PHP Warning: date(): It is not safe to rely on the system's timezone...

php获取域名的google收录示例

复制代码 代码如下: function get_index($domain){ $url="http://www.google.com/search?source=hp&biw=1440...

如何使用PHP计算上一个月的今天

一日,遇到一个问题,求上一个月的今天。 最开始我们使用 strtotime(”-1 month”) 函数求值,发现有一个问题,月长度不一样的月份的计算结果有误。 比如:2011-03-3...

php不用GD库生成当前时间的PNG格式图象的程序第1/2页

<?php function set_4pixel($r, $g, $b, $x, $y) { global $sx, $sy, $pixels; $ofs = 3 * ($sx...