PHP字符串与字节转换示例

WBOY
Lepaskan: 2016-08-08 09:26:29
asal
1865 orang telah melayarinya

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>
Salin selepas log masuk

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan