PHP-php 求一串纯中文字符串转化成 二进制和十进制的算法

PHP-php 求一串纯中文字符串转化成 二进制和十进制的算法

浮生未歇 发布于 2016-11-24 字数 164 浏览 1254 回复 5

求一串纯中文字符串转化成 二进制和十进制的算法,实现互换的算法。给出一串中文字符串实现二进制和十进制转化,相反给出二进制和十进制能转化成中文字符串。十分感谢!

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

灵芸 2017-08-13 5 楼

几年前写过这么一个工具类 找出来了 你可以在我这个基础上再包一层来实现 二进制转化 和 逆反
当前这样 在浏览器 能自动转码 :-)

 <?php
/**
* 时间: 2009-4-9 17:19:55
* iamsese.cn 我是色色
* vb2005xu.javaeye.com
*
* Unicode 编码和转换功能库
*/

class IAMSESE_UNICODE {

/**
* ord 方法的unicode 支持
*
* @param 单字 $c
* @return int
*/
function uniord($c) {
$h = ord($c{0});
if ($h <= 0x7F) {
return $h;
} else if ($h < 0xC2) {
return false;
} else if ($h <= 0xDF) {
return ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
} else if ($h <= 0xEF) {
return ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6
| (ord($c{2}) & 0x3F);
} else if ($h <= 0xF4) {
return ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12
| (ord($c{2}) & 0x3F) << 6
| (ord($c{3}) & 0x3F);
} else {
return false;
}
}

/**
* 将指定编码的字符串分解成数组
*
* @param STRING $string
* @param STRING $encode
* @return ARRAY
*/
function mbStringToArray ($string,$encode="UTF-8") {
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string,0,1,$encode);
$string = mb_substr($string,1,$strlen,$encode);
$strlen = mb_strlen($string);
}
return $array;
}

/**
* 仅支持PHP5 -- 同上,是icov实现
*/
// function iconvStringToArray ($string,$encode="UTF-8") {
// $strlen = iconv_strlen($string,$encode);
// while ($strlen) {
// $array[] = iconv_substr($string,0,1,$encode);
// $string = iconv_substr($string,1,$strlen,$encode);
// $strlen = iconv_strlen($string,$encode);
// }
// return $array;
// }

/**
* Unicode 编码表示
*
* @param 单字 $unichar
* @return String
*/
function unicharCodeAt($unichar){
return "&#" . IAMSESE_UNICODE::uniord($unichar) . ';' ;
}

/**
* chr 函数的unicode实现
*
* @param mixed[整形数组或者整数] $codes
* @return String
*/
function uchr ($codes) {
if (is_scalar($codes))
$codes= func_get_args();
$str= '';
foreach ($codes as $code)
$str.= html_entity_decode('&#'.$code.';',ENT_NOQUOTES,'UTF-8');
return $str;
}

/*
* 同上 基于mb实现
*/
// function unichr($codes) {
// if (is_scalar($codes))
// $codes= func_get_args();
// $str= '';
// foreach ($codes as $code)
// $str.= mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
// return $str;
// }

/**
* 将unicode编码的字符串解析成正常的数据
*
* @param String $str
* @return String
*/
function deUnicode($str){
$arr = explode (';',str_ireplace('&#','',$str) );
//去除最后一个空字符
$arr = array_slice($arr,0,count($arr)-1);
return IAMSESE_UNICODE::uchr($arr);
}

/**
* 将字符串解析成unicode编码的字符串
*
* @param String $str
* @return String
*/
function encode($str){
$arr = IAMSESE_UNICODE::mbStringToArray($str);
$str = '' ;
foreach($arr as $a){
$str .= IAMSESE_UNICODE::unicharCodeAt($a) ;
}
return $str ;
}

/**
* 输出Unicode字符表
*
*/
function echoUnicodeTable(){
for ($i=0;$i<4096;$i++){
echo '<br/>' . dechex($i). ' ********** ';
for ($j=0;$j<16;$j++){
$ch = intval($i * 16 + $j);
$ch = IAMSESE_UNICODE::uchr($ch);
echo $ch . ' -- ';
}
}
}

/**
* 输出Ascii字符表
*
*/
function echoAsciiTable(){

for($i=0;$i<16;$i++){
echo '<br/>' . ($i). ' ********** ';
for ($j=0;$j<16;$j++){
$num = intval($i * 16 + $j);
$ch = IAMSESE_UNICODE::uchr($num);
echo "{$ch} --- ";
}
}
}

function test(){
$test1 ='我是色色[iamsese.cn] начинается уже в марте bbs.cn #$%^&*()_+!@QADD?><.,,m';
$test1 = IAMSESE_UNICODE::encode($test1);
echo $test1 ;
echo "n<br/>n";
$test1 = IAMSESE_UNICODE::deUnicode($test1);
echo $test1 ;
}

}

IAMSESE_UNICODE::test();
//IAMSESE_UNICODE::echoUnicodeTable();
IAMSESE_UNICODE::echoAsciiTable();
?>

运行结果

 我是色色[iamsese.cn] начинается уже в марте bbs.cn #$%^&*()_+!@QADD?><.,,m
<br/>
我是色色[iamsese.cn] начинается уже в марте bbs.cn #$%^&*()_+!@QADD?><.,,m<br/>0 ********** � --- --- --- --- --- --- --- --- --- ---
--- --- ---
--- --- --- <br/>1 ********** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- <br/>2 ********** --- ! --- " --- # --- $ --- % --- & --- ' --- ( --- ) --- * --- + --- , --- - --- . --- / --- <br/>3 ********** 0 --- 1 --- 2 --- 3 --- 4 --- 5 --- 6 --- 7 --- 8 --- 9 --- : --- ; --- < --- = --- > --- ? --- <br/>4 ********** @ --- A --- B --- C --- D --- E --- F --- G --- H --- I --- J --- K --- L --- M --- N --- O --- <br/>5 ********** P --- Q --- R --- S --- T --- U --- V --- W --- X --- Y --- Z --- [ --- --- ] --- ^ --- _ --- <br/>6 ********** ` --- a --- b --- c --- d --- e --- f --- g --- h --- i --- j --- k --- l --- m --- n --- o --- <br/>7 ********** p --- q --- r --- s --- t --- u --- v --- w --- x --- y --- z --- { --- | --- } --- ~ ---  --- <br/>8 ********** € ---  --- ‚ --- ƒ --- „ --- … --- † --- ‡ --- ˆ --- ‰ --- Š --- ‹ --- Œ ---  --- Ž ---  --- <br/>9 **********  --- ‘ --- ’ --- “ --- ” --- • --- – --- — --- ˜ --- ™ --- š --- › --- œ ---  --- ž --- Ÿ --- <br/>10 ********** --- ¡ --- ¢ --- £ --- ¤ --- ¥ --- ¦ --- § --- ¨ --- © --- ª --- « --- ¬ --- ­ --- ® --- ¯ --- <br/>11 ********** ° --- ± --- ² --- ³ --- ´ --- µ --- ¶ --- · --- ¸ --- ¹ --- º --- » --- ¼ --- ½ --- ¾ --- ¿ --- <br/>12 ********** À --- Á ---  --- à --- Ä --- Å --- Æ --- Ç --- È --- É --- Ê --- Ë --- Ì --- Í --- Î --- Ï --- <br/>13 ********** Ð --- Ñ --- Ò --- Ó --- Ô --- Õ --- Ö --- × --- Ø --- Ù --- Ú --- Û --- Ü --- Ý --- Þ --- ß --- <br/>14 ********** à --- á --- â --- ã --- ä --- å --- æ --- ç --- è --- é --- ê --- ë --- ì --- í --- î --- ï --- <br/>15 ********** ð --- ñ --- ò --- ó --- ô --- õ --- ö --- ÷ --- ø --- ù --- ú --- û --- ü --- ý --- þ --- ÿ ---

灵芸 2017-06-11 4 楼

先把字符串分割成数组的形式,然后把中文转换成数字,再进行二进制、十进制和十六进制转换

function getUnicodeFromOneUTF8($word) {
if (is_array( $word))
$arr = $word;
else
$arr = str_split($word);
//此时,$arr应类似array(228, 189, 160)
//定义一个空字符串存储
$bin_str = '';
//转成数字再转成二进制字符串,最后联合起来。
foreach ($arr as $value)
$bin_str .= decbin(ord($value));
//此时,$bin_str应类似111001001011110110100000,如果是汉字"你"
//正则截取
$bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3', $bin_str);

return bindec($bin_str);
//返回类似20320, 汉字"你"
//return dechex(bindec($bin_str));
//如想返回十六进制4f60,用这句
}

泛泛之交 2017-05-15 3 楼

 <?php
/**
* 把一个汉字转为unicode的通用函数,不依赖任何库,和别的自定义函数,但有条件
* 条件:本文件以及函数的输入参数应该用utf-8编码,不然要加函数转换
* 其实亦可轻易编写反向转换的函数,甚至不局限于汉字,奇怪为什么PHP没有现成函数
* @author xieye
*
* @param {string} $word 必须是一个汉字,或代表汉字的一个数组(用str_split切割过)
* @return {string} 一个十进制unicode码,如4f60,代表汉字 “你”
*
* @example
echo "你 ".getUnicodeFromOneUTF8("你");
echo "<br />";
echo "好 ".getUnicodeFromOneUTF8("好");
echo "<br />";
echo "你好 ".getUnicodeFromOneUTF8("你好");
echo "<br />";
echo "你好吗 ".getUnicodeFromOneUTF8("你好吗");
你 20320
好 22909
你好 251503099357000
你好吗 4.21952182258E+21
*/
function getUnicodeFromOneUTF8($word) {
//获取其字符的内部数组表示,所以本文件应用utf-8编码!
if (is_array( $word))
$arr = $word;
else
$arr = str_split($word);
//此时,$arr应类似array(228, 189, 160)
//定义一个空字符串存储
$bin_str = '';
//转成数字再转成二进制字符串,最后联合起来。
foreach ($arr as $value)
$bin_str .= decbin(ord($value));
//此时,$bin_str应类似111001001011110110100000,如果是汉字"你"
//正则截取
$bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3', $bin_str);

//此时, $bin_str应类似0100111101100000,如果是汉字"你"
return bindec($bin_str);
//返回类似20320, 汉字"你"
//return dechex(bindec($bin_str));
//如想返回十六进制4f60,用这句
}
echo "你 ".getUnicodeFromOneUTF8("你");
echo "<br />";
echo "好 ".getUnicodeFromOneUTF8("好");
echo "<br />";
echo "你好 ".getUnicodeFromOneUTF8("你好");
echo "<br />";
echo "你好吗 ".getUnicodeFromOneUTF8("你好吗");
exit;

?>

来自:http://blog.csdn.net/nailwl/article/details/5520068

瑾兮 2017-03-16 2 楼

先说下我的实现原理,通过urlencode()将汉字转换为十六进制,在通过base_convert()来进行任意进制之间的转换。

那么单个汉字可以做下边的操作:

$str = "我";

 $hex = str_replace("%",'',urlencode($str)); //十六进制
$decimal = base_convert(str_replace("%",'',urlencode($str)),16,10); //十进制
$octal = base_convert(str_replace("%",'',urlencode($str)),16,8); //八进制
$binary = base_convert(str_replace("%",'',urlencode($str)),16,2); //二进制

//反转拿二进制举例 其他都一样
echo urldecode("%".join("%",str_split(base_convert($binary,2,16),2)));

单个字实现了,那么一串字的话,实现方式就很简单了,下边用八进制来举例:

 $str = "我是中国人";
for($i=0;$i<strlen($str);$i+=3) {
$hexStr = urlencode(substr($str,$i,3));
$octal .= base_convert(str_replace("%",'',$hexStr),16,8)."-"; //八进制
}
//反转
$str1 = '';
foreach(explode("-",rtrim($octal,'-')) as $o) {
$str1 .= urldecode("%".join("%",str_split(base_convert($o,8,16),2)));
}

echo $octal,"<br />n";
echo $str1;

需要注意的是,我这里都是按照utf-8编码的文字处理的,如果是gbk,那么就是两个字节为一个汉字。

归属感 2017-03-05 1 楼
$string = "博客"; 
$length = strlen($string); 
echo $string; 
$result = array(); 
//十进制 
for($i=0;$i<$length;$i++){ 
if(ord($string[$i])>127){ 
$result[] = ord($string[$i]).' '.ord($string[++$i]); 
} 
} 
echo '十进制:';var_dump($result); 
echo "<br />";
//二进制
$strings = array(); 
foreach($result as $v){ 
$dec = explode(" ",$v); 
$strings[] = decbin($dec[0])." ".decbin($dec[1]); 
} 
echo '二进制:';var_dump($strings); 
echo "<br />";
//十六进制 
$strings = array(); 
foreach($result as $v){ 
$dec = explode(" ",$v); 
$strings[] = dechex($dec[0])." ".dechex($dec[1]); 
} 
echo '十六进制:';var_dump($strings); 
echo "<br />";

$hz = '';
foreach($strings as $v){ 
$dec = explode(" ",$v); 
$hz .= $dec[0].$dec[1]; 
}
$hz = hexDecode($hz);
var_dump($hz); 

//16进制编码转换为汉字
function hexDecode($s) {
return preg_replace('/(w{2})/e',"chr(hexdec('\1'))",$s);
}