ホームページ  >  記事  >  バックエンド開発  >  PHP文字列からバイトへの変換例

PHP文字列からバイトへの変換例

WBOY
WBOYオリジナル
2016-08-08 09:26:291866ブラウズ

参照: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 までご連絡ください。