Heim  >  Artikel  >  Backend-Entwicklung  >  Beispiel für die Konvertierung von PHP-Strings in Bytes

Beispiel für die Konvertierung von PHP-Strings in Bytes

WBOY
WBOYOriginal
2016-08-08 09:26:291866Durchsuche

siehe: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>

Das Obige stellt die PHP-String- und Byte-Konvertierungsbeispiele einschließlich des relevanten Inhalts vor. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:微信公众平台开发尝试Nächster Artikel:对PHPstatic的理解