php字符串与byte字节数组转化类示例_PHP教程

WBOY
Libérer: 2016-07-13 10:44:16
original
929 Les gens l'ont consulté

文章给大家提供一个php字符串与byte字节数组转化类示例,希望文章对各位同学会有所帮助。

 代码如下 复制代码
 
/**
 
* byte数组与字符串转化类
 
*/
 
class Bytes {
 
   
/**
    
* 转换一个String字符串为byte数组
    
* @param $str 需要转换的字符串
    
* @param $bytes 目标byte数组
    
* @author Zikie
    
*/
    public static function getBytes($string) {
        $bytes = array();
        for($i = 0; $i              $bytes[] = ord($string[$i]);
        }
        return $bytes;
    }
 
   
/**
    
* 将字节数组转化为String类型的数据
    
* @param $bytes 字节数组
    
* @param $str 目标字符串
    
* @return 一个String类型的数据
    
*/
 
    public static function toStr($bytes) {
        $str = '';
        foreach($bytes as $ch) {
            $str .= chr($ch);
        }
 
           return $str;
    }
 
   
/**
    
* 转换一个int为byte数组
    
* @param $byt 目标byte数组
    
* @param $val 需要转换的字符串
    
*
    
*/
 
    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;
    }
 
   
/**
    
* 从字节数组中指定的位置读取一个Integer类型的数据
    
* @param $bytes 字节数组
    
* @param $position 指定的开始位置
    
* @return 一个Integer类型的数据
    
*/
 
    public static function bytesToInteger($bytes, $position) {
        $val = 0;
        $val = $bytes[$position + 3] & 0xff;
        $val         $val |= $bytes[$position + 2] & 0xff;
        $val         $val |= $bytes[$position + 1] & 0xff;
        $val         $val |= $bytes[$position] & 0xff;
        return $val;
    }
 
   
/**
    
* 转换一个shor字符串为byte数组
    
* @param $byt 目标byte数组
    
* @param $val 需要转换的字符串
    
*
    
*/
 
    public static function shortToBytes($val) {
        $byt = array();
        $byt[0] = ($val & 0xff);
        $byt[1] = ($val >> 8 & 0xff);
        return $byt;
    }
 
   
/**
    
* 从字节数组中指定的位置读取一个Short类型的数据。
    
* @param $bytes 字节数组
    
* @param $position 指定的开始位置
    
* @return 一个Short类型的数据
    
*/
 
    public static function bytesToShort($bytes, $position) {
        $val = 0;
        $val = $bytes[$position + 1] & 0xFF;
        $val = $val         $val |= $bytes[$position] & 0xFF;
        return $val;
    }
 
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633109.htmlTechArticle文章给大家提供一个php字符串与byte字节数组转化类示例,希望文章对各位同学会有所帮助。 代码如下 复制代码 ?php /** * byte数组与字符串转...
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!