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实现的生成排列算法示例

本文实例讲述了php实现的生成排列算法。分享给大家供大家参考,具体如下: <?php function perm($s, $n, $index) { if($n =...

PHP实现自动登入google play下载app report的方法

本文实例讲述了PHP实现自动登入google play下载app report的方法,有不错的实用价值。分享给大家供大家参考。具体实现步骤如下: 一、流程: 1.登入google pla...

PHP迭代器和迭代的实现与使用方法分析

本文实例讲述了PHP迭代器和迭代的实现与使用方法。分享给大家供大家参考,具体如下: PHP的面向对象引擎提供了一个非常聪明的特性,就是,可以使用foreach()方法通过循环方式取出一个...

说说PHP的autoLoad自动加载机制

说说PHP的autoLoad自动加载机制

__autoload的使用方法1: 最经常使用的就是这种方法,根据类名,找出类文件,然后require_one 复制代码 代码如下: function __autoload($class...

php根据年月获取当月天数及日期数组的方法

本文实例讲述了php根据年月获取当月天数及日期数组的方法。分享给大家供大家参考,具体如下: function get_day( $date ) { $tem = expl...