php 主機序和網路序轉換 (ntohl、htonl、ntohs、ntohs)
阿新 • • 發佈:2018-12-06
function ntohl($str) { $res = unpack('I', pack('N', $str)); return $res[1]; } function htonl($str) { $res = unpack('N', pack('I', $str)); return $res[1]; } function ntohs($str) { $res = unpack('S', pack('n', $str)); return $res[1]; } function htons($str) { $res = unpack('n', pack('S', $str)); return $res[1]; } function convert_integer(){ $data = 2048; printf("Original: %s => Long host byte order: %s, Network byte order: %s\n", $data, ntohl($data), htonl($data)); echo "<br />"; printf("Original: %s => Short host byte order: %s, Network byte order: %s\n", $data, ntohs($data), htons($data)); } convert_integer();