Home > Backend Development > PHP Tutorial > PHP string and byte array conversion class example

PHP string and byte array conversion class example

WBOY
Release: 2016-07-29 09:10:43
Original
3870 people have browsed it

phpString and byteByte arrayTransformation class example

php 
  
/**

* bytearray and string conversionclassification

*/
  
class Bytes { 
  
    
/**

* Convert to a String string to byte array

* @para m $str needs to convertcharacters String

* @param $bytes Target byteArray

* @author Zikie

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

* Convert the byte array to into String type data

* @param $bytes byte array

* @param $str target String

* @return A String type data

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

* ConvertChange an int to bytearray
Array
* @param $ val needs to
convert to change the
string*/  
    public static function integerToBytes($val) { 
        $byt = array(); 
        $byt[0] = ($val & 0xff); 
        $byt[1] = ($val >> 8 & 0xff); 
        $byt[2] = ($val >> 16 & 0xff); 
        $byt[3] = ($val >> 24 & 0xff); 
        return $byt; 
    } 
  
    
/**

* Read an Integer type data from the specified position in the byte array

* @param $bytes byte array

* @param $ The start specified by position Location

* @return an Integer type data

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

* Convert to a shor string to byte array

* @para m $byt targetbytearray
​​​
* @param $val needs to convert to change the string
*/
  
    public static function shortToBytes($val) { 
        $byt = array(); 
        $byt[0] = ($val & 0xff); 
        $byt[1] = ($val >> 8 & 0xff); 
        return $byt; 
    } 
  
    
/**

* Read a Short type data from the specified position in the byte array.

* @param $bytes byte array

* @param $position specified starting position

* @return a Short type Data of

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template