首頁  >  文章  >  後端開發  >  PHP字串與位元組轉換範例

PHP字串與位元組轉換範例

WBOY
WBOY原創
2016-08-08 09:26:291866瀏覽

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> < strlen(<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> <<= <span>8</span>; 
        <span>$val</span> |= <span>$bytes</span>[<span>$position</span> + <span>2</span>] & <span>0xff</span>; 
        <span>$val</span> <<= <span>8</span>; 
        <span>$val</span> |= <span>$bytes</span>[<span>$position</span> + <span>1</span>] & <span>0xff</span>; 
        <span>$val</span> <<= <span>8</span>; 
        <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> << <span>8</span>; 
        <span>$val</span> |= <span>$bytes</span>[<span>$position</span>] & <span>0xFF</span>; 
        <span>return</span><span>$val</span>; 
    } 

} 
<span>?></span></span></code>

以上就介紹了PHP字串與位元組轉換範例,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn