TCP常用方法

//格式化为16进制输出指令
function fromateSendCode($code){
    $codeArr = getCodeWithSpace($code);
    for($i=0; $i<count($codeArr); $i++){
        $cmdHex[] = chr(hexdec($codeArr[$i]));
    }
    $cmdHex = implode($cmdHex);
    return $cmdHex;
}

//格式化收到指令
function formateRevice($data){
    $receiveData = str_split(bin2hex($data), 2);
    $s='';
    foreach ($receiveData as $value) {
        $s.=$value;
    }
    return $s;
}
/**
 * 对16进制支付串进行和运算 和校验
 * @param  string $string 16进制字符串
 * @return int 16进制
 */
function converTo16Space($codeArr){
    $s = '';
    foreach ($codeArr as $value) {
        $dec = hexdec($value);
        if($s){
            $s = $s+$dec;//&异或校验
        }else{
            $s = $dec;
        }
    }
    return substr(strtoupper(dechex($s)),-2);
}

 

另附通过韦根26协议转8位十进制方法

//卡号转八位十六进制
//http://www.chinadmd.com/file/zwixiuwxsxxwiatcauevazor_1.html
function cardToHex($cardNo){
    $head = substr($cardNo, 0, 3);
    $body = substr($cardNo, -5);
    $hexHead = dechex($head);
    $hexBody = dechex($body);
    $hexBody = substr($hexBody, -2).substr($hexBody, 0, 2);
    $hex = $hexBody.$hexHead;
    $hex = strtoupper(str_pad($hex, 8, '0', STR_PAD_RIGHT ));
    return $hex;
}

 

posted @ 2018-06-27 21:48  limes  阅读(456)  评论(0编辑  收藏  举报