当前位置 : 主页 > 网络编程 > PHP >

php字符串和asscii码互转

来源:互联网 收集:自由互联 发布时间:2021-06-30
//把字符串转化为asscii码function Binary2String($pIn){ $str = ''; $iOut=0; for($i = 0 ;$istrlen($pIn);$i++){ $str .= Value2Ascii((ord($pIn[$i])0xF0)4); $str .= Value2Ascii(ord($pIn[$i])0x0F); } return $str;}function Value2Ascii($in){
 
//把字符串转化为asscii码
function  Binary2String($pIn){
    $str = '';
    $iOut=0;
    for($i = 0 ;$i<strlen($pIn);$i++){
  
        $str .= Value2Ascii((ord($pIn[$i])&0xF0)>>4);
  
        $str .= Value2Ascii(ord($pIn[$i])&0x0F);
    }
    return $str;
}
function Value2Ascii($in){
      
    switch($in){
        case 0:
            return '0';
            break;
        case 1:
            return '1';
            break;
        case 2:
            return '2';
            break;
        case 3:
            return '3';
            break;
        case 4:
            return '4';
            break;
        case 5:
            return '5';
            break;
        case 6:
            return '6';
            break;
        case 7:
            return '7';
            break;
        case 8:
            return '8';
            break;
        case 9:
            return '9';
            break;
        case 10:
            return 'A';
            break;
        case 11:
            return 'B';
            break;
        case 12:
            return 'C';
            break;
        case 13:
            return 'D';
            break;
        case 14:
            return 'E';
            break;
        case 15:
            return 'F';
            break;
        default:
              
            return '?';
            break;
    }
    return '?';
}
  
//把asscii码转化为字符串
function DBStr2Binary($pIn){
  
    $iOut = 0;
    $pOut = '';
    for( $i = 0 ;$i<strlen($pIn)-1;){
        if($pIn[$i]=='\\0'|| $pIn[$i+1]=='\\0'){
            break;
        }
        $pOut   .=  chr((Ascii2Value($pIn[$i])<<4) + (Ascii2Value($pIn[$i+1])));
        $iOut++;
        $i+=2;
          
    }
    return $pOut;
}
function Ascii2Value($in){
      
        switch($in){
        case '0':
            return 0;
            break;
        case '1':
            return 1;
            break;
        case '2':
            return 2;
            break;
        case '3':
            return 3;
            break;
        case '4':
            return 4;
            break;
        case '5':
            return 5;
            break;
        case '6':
            return 6;
            break;
        case '7':
            return 7;
            break;
        case '8':
            return 8;
            break;
        case '9':
            return 9;
            break;
        case 'A':
            return 10;
            break;
        case 'B':
            return 11;
            break;
        case 'C':
            return 12;
            break;
        case 'D':
            return 13;
            break;
        case 'E':
            return 14;
            break;
        case 'F':
            return 15;
            break;
        default:
              
            return '?';
            break;
    }
        return '?';
}
  
$str = '伟大的中国程序猿!';
$bin = Binary2String($str);
echo $bin.'<br />';
$new_str = DBStr2Binary($bin);
echo $new_str;

网友评论