Home > Backend Development > PHP Tutorial > php实现二进制和文本相互转换的方法_PHP

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

WBOY
Release: 2016-05-30 15:09:33
Original
1019 people have browsed it

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

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

<&#63;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&#63;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&#63;');
&#63;>
Copy after login

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template