PHP字符串与字节转换示例

WBOY
发布: 2016-08-08 09:26:29
原创
1865 人浏览过

refer:http://www.php230.com/1410667081.html

<code><span><span><?php </span><span>/** 

* byte数组与字符串转化类 

*/</span><span><span>class</span><span>Bytes</span> {</span><span>/** 

* 转换一个String字符串为byte数组 

*<span> @param</span> $str 需要转换的字符串 

*<span> @param</span> $bytes 目标byte数组 

*<span> @author</span> Zikie 

*/</span><span>public</span><span>static</span><span><span>function</span><span>getBytes</span><span>(<span>$string</span>)</span> {</span><span>$bytes</span> = <span>array</span>(); 
        <span>for</span>(<span>$i</span> = <span>0</span>; <span>$i</span> $string</span>); <span>$i</span>++){ 
             <span>$bytes</span>[] = ord(<span>$string</span>[<span>$i</span>]); 
        } 
        <span>return</span><span>$bytes</span>; 
    } 


<span>/** 

* 将字节数组转化为String类型的数据 

*<span> @param</span> $bytes 字节数组 

*<span> @param</span> $str 目标字符串 

*<span> @return</span> 一个String类型的数据 

*/</span><span>public</span><span>static</span><span><span>function</span><span>toStr</span><span>(<span>$bytes</span>)</span> {</span><span>$str</span> = <span>''</span>; 
        <span>foreach</span>(<span>$bytes</span><span>as</span><span>$ch</span>) { 
            <span>$str</span> .= chr(<span>$ch</span>); 
        } 

           <span>return</span><span>$str</span>; 
    } 


<span>/** 

* 转换一个int为byte数组 

*<span> @param</span> $byt 目标byte数组 

*<span> @param</span> $val 需要转换的字符串 

* 

*/</span><span>public</span><span>static</span><span><span>function</span><span>integerToBytes</span><span>(<span>$val</span>)</span> {</span><span>$byt</span> = <span>array</span>(); 
        <span>$byt</span>[<span>0</span>] = (<span>$val</span> & <span>0xff</span>); 
        <span>$byt</span>[<span>1</span>] = (<span>$val</span> >> <span>8</span> & <span>0xff</span>); 
        <span>$byt</span>[<span>2</span>] = (<span>$val</span> >> <span>16</span> & <span>0xff</span>); 
        <span>$byt</span>[<span>3</span>] = (<span>$val</span> >> <span>24</span> & <span>0xff</span>); 
        <span>return</span><span>$byt</span>; 
    } 


<span>/** 

* 从字节数组中指定的位置读取一个Integer类型的数据 

*<span> @param</span> $bytes 字节数组 

*<span> @param</span> $position 指定的开始位置 

*<span> @return</span> 一个Integer类型的数据 

*/</span><span>public</span><span>static</span><span><span>function</span><span>bytesToInteger</span><span>(<span>$bytes</span>, <span>$position</span>)</span> {</span><span>$val</span> = <span>0</span>; 
        <span>$val</span> = <span>$bytes</span>[<span>$position</span> + <span>3</span>] & <span>0xff</span>; 
        <span>$val</span> 8</span>; 
        <span>$val</span> |= <span>$bytes</span>[<span>$position</span> + <span>2</span>] & <span>0xff</span>; 
        <span>$val</span> 8; 
        <span>$val</span> |= <span>$bytes</span>[<span>$position</span> + <span>1</span>] & <span>0xff</span>; 
        <span>$val</span> 8; 
        <span>$val</span> |= <span>$bytes</span>[<span>$position</span>] & <span>0xff</span>; 
        <span>return</span><span>$val</span>; 
    } 


<span>/** 

* 转换一个shor字符串为byte数组 

*<span> @param</span> $byt 目标byte数组 

*<span> @param</span> $val 需要转换的字符串 

* 

*/</span><span>public</span><span>static</span><span><span>function</span><span>shortToBytes</span><span>(<span>$val</span>)</span> {</span><span>$byt</span> = <span>array</span>(); 
        <span>$byt</span>[<span>0</span>] = (<span>$val</span> & <span>0xff</span>); 
        <span>$byt</span>[<span>1</span>] = (<span>$val</span> >> <span>8</span> & <span>0xff</span>); 
        <span>return</span><span>$byt</span>; 
    } 


<span>/** 

* 从字节数组中指定的位置读取一个Short类型的数据。 

*<span> @param</span> $bytes 字节数组 

*<span> @param</span> $position 指定的开始位置 

*<span> @return</span> 一个Short类型的数据 

*/</span><span>public</span><span>static</span><span><span>function</span><span>bytesToShort</span><span>(<span>$bytes</span>, <span>$position</span>)</span> {</span><span>$val</span> = <span>0</span>; 
        <span>$val</span> = <span>$bytes</span>[<span>$position</span> + <span>1</span>] & <span>0xFF</span>; 
        <span>$val</span> = <span>$val</span> 8; 
        <span>$val</span> |= <span>$bytes</span>[<span>$position</span>] & <span>0xFF</span>; 
        <span>return</span><span>$val</span>; 
    } 

} 
<span>?></span></code>
登录后复制

以上就介绍了PHP字符串与字节转换示例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板