Chinaunix首页 | 论坛 | 博客
  • 博客访问: 434437
  • 博文数量: 50
  • 博客积分: 5071
  • 博客等级: 大校
  • 技术积分: 1780
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-20 10:36
文章分类

全部博文(50)

文章存档

2011年(2)

2010年(6)

2009年(12)

2008年(30)

我的朋友

分类:

2008-06-09 21:54:39

字符串中只能包含字符,特殊符号不行。


//字符串转化为ascii

function str2asc($str)
{
    
$num='';
    
while(strlen($str)!=0)
    {
        
$a=substr($str,0,1);
        
$num.=ord($a);
        
$str=substr($str,1,strlen($str));
    }
    
return $num;
}



//ascii转化为字符串

function asc2str($num)
{
    
$num=(string)$num;
    
$str='';
    
while(strlen($num)!=0)
    {
        
$a=(int)substr($num,0,1);
        
if($a>2)
        {
            
$b=(int)substr($num,0,2);
            
$num=substr($num,2,strlen($num));
        }
else
        {
            
$b=(int)substr($num,0,3);
            
$num=substr($num,3,strlen($num));
        }
        
$str.=chr($b);
    }
    
return $str;
}

示例:
echo str2asc( 'sfdsl ');
echo  '
';
echo asc2str(str2asc( 'sfdsl '));
---------------------
115102100115108
sfdsl 

注:调用asc2str( '123456 ') 不能asc2str(123456) ,也就是要将123456转化为字符串。

function asc2bin($str) {
$text_array = explode("\r\n", chunk_split($str, 1));
for ($n = 0; $n < count($text_array) - 1; $n++) {
$newstring .= substr("0000".base_convert(ord($text_array[$n]), 10, 2), -8);
}
$newstring = chunk_split($newstring, 8, " ");
return $newstring;
}

function bin2asc($str) {
$str = str_replace(" ", "", $str);
$text_array = explode("\r\n", chunk_split($str, 8));
for ($n = 0; $n < count($text_array) - 1; $n++) {
$newstring .= chr(base_convert($text_array[$n], 2, 10));
}
return $newstring;
}

以上两个函数是实现asc和binary之间的幻想转化的。

重点说几个函数吧。

其中的chunk_split函数的原型如下:

string chunk_split(string string, int [chunklen] , string [end]);

函数描述:

本函数将字符串变成小段供其它函数使用。例如,base64_encode。内定是参数 chunklen (76 个字符) 每隔 76 个字符插入 end ("\r\n")。传回新字符串而不更动原字串。

举个简单的例子:


$string=chunk_split("abcdefghijklmnopqrstuvwxyz",5);
echo $string;

?>
将会显示如下格式的字符串:
abcde fghij klmno pqrst uvwxy z



还有一个需要注意的是base_convert函数。它是一个数学函数。函数描述如下:

string base_convert(string number, int frombase, int tobase);

本函式将数字字串 number 从以 frombase 进位转换到以 tobase 进位。本式能够处理的由以二进位到以三十六进位之间的进位方式。在十进位之前都是以数字表示,而在超过十进位之后就用英文字母表示。例如十六进位个位数依序为 123456789abcdef,10 的顺序是第十七个,这时才进一位。而三十六进位 a 是第十个、b 为第十一个、z 为第三十六个、10 是第三十七个,这时才进位。

弄懂了上面两个函数,我给出的转换函数相信大家也能看明白了。。。

我们接着看:

function asc2hex($str) {
return chunk_split(bin2hex($str), 2, " ");
}
function hex2asc($str) {
$str = str_replace(" ", "", $str);
for ($n=0; $n$newstring .= pack("C", hexdec(substr($str, $n, 2)));
}

return $newstring;
}

以上实现了asc到十六进制之间的转换。第一个函数很简单,我就不多说了。第二个函数需要注意下面2个函数:

string str_replace(string needle, string str, string haystack);

这个函数是将haystack字符串中的needle子串替换成str字符串。

pack函数在perl时代就是一个非常有用的函数,在php里虽然没有那么重要但是也不可小视的。
string pack(string format, mixed [args]...);

把一个列表或数组以在实际机器存贮格式或C等编程语言使用的格式转化(包装)到一个简单变量中。
具体用法大家可以参考perl或者php中的函数说明。


再来:


function binary2hex($str) {
$str = str_replace(" ", "", $str);
$text_array = explode("\r\n", chunk_split($str, 8));
for ($n = 0; $n < count($text_array) - 1; $n++) {
$newstring .= base_convert($text_array[$n], 2, 16);
}
$newstring = chunk_split($newstring, 2, " ");
return $newstring;
}

function hex2binary($str) {
$str = str_replace(" ", "", $str);
$text_array = explode("\r\n", chunk_split($str, 2));
for ($n = 0; $n < count($text_array) - 1; $n++) {
$newstring .= substr("0000".base_convert($text_array[$n], 16, 2), -8);
}
$newstring = chunk_split($newstring, 8, " ");
return $newstring;
}


上面两个是对一六进制和二进制之间的转换。大部分函数都讲过了。很简单。


看到这里,大家对php的柑橘而是什么呢?我想不只是强大可以形容吧。

阅读(593) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~