메뉴 건너뛰기

Dev tips

PHP 유용한 함수 pack, unpack, ip2long, long2ip

taknim 2009.01.13 01:30 조회 수 : 33346 추천:1998


pack

(PHP 3, PHP 4 , PHP 5)

pack -- Pack data into binary string.

Description

string pack ( string format [, mixed args])

Pack given arguments into binary string according to format. Returns binary string containing data.

The idea to this function was taken from Perl and all formatting codes work the same as there, however, there are some formatting codes that are missing such as Perl's "u" format code. The format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data. For a, A, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string. Currently implemented are

표 1. pack() format characters

Code Description
a NUL-padded string
A SPACE-padded string
h Hex string, low nibble first
H Hex string, high nibble first
c signed char
C unsigned char
s signed short (always 16 bit, machine byte order)
S unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
I unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
L unsigned long (always 32 bit, machine byte order)
N unsigned long (always 32 bit, big endian byte order)
V unsigned long (always 32 bit, little endian byte order)
f float (machine dependent size and representation)
d double (machine dependent size and representation)
x NUL byte
X Back up one byte
@ NUL-fill to absolute position

예 1. pack() example

<?php
$binarydata
= pack("nvc*", 0x1234, 0x5678, 65, 66);
?>

The resulting binary string will be 6 bytes long and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.

Note that the distinction between signed and unsigned values only affects the function unpack(), where as function pack() gives the same result for signed and unsigned format codes.

Also note that PHP internally stores integer values as signed values of a machine dependent size. If you give it an unsigned integer value too large to be stored that way it is converted to a float which often yields an undesired result.

번호 제목 글쓴이 날짜 조회 수
313 구글을 이용한 rapidshare 에 등록된 파일 검색 taknim 2009.06.19 9403
312 폼관련 스크립트 짤때 맨날 인클루드 해서쓰는 함수들 taknim 2009.06.17 7446
311 [객체지향] this / __get() / __set() / clone / autoload / :: taknim 2009.06.12 9255
310 [Oracle] ORA-00600, ORA-06544, ORA-06553 오류 발생시 taknim 2009.06.09 23834
309 [php.ini] auto_prepend_file, auto_append_file 설정 taknim 2009.06.04 11648
308 IE6 에서 SELECT z-index 문제 taknim 2009.06.04 10207
307 리눅스에서 삼바서버 연결하기, 끊기 (mount, umount) taknim 2009.05.29 12319
306 VNC사용시 number keypad숫자가 안 먹을때 taknim 2009.05.29 11947
305 문자열에서 문자 하나하나를 html 형식(&#)으로 변경하기 taknim 2009.05.14 8013
304 이미 설치된 아파치에 rewrite 모듈 추가 설치 taknim 2009.04.30 11878
303 MS-SQL 링크드 서버 만들기 taknim 2009.03.20 12175
302 특정 레코드 앞뒤로 합계 구하기(over,range,PRECEDING,following) taknim 2009.02.05 9909
301 StrConv 함수 - 로케일 변환 taknim 2009.01.30 10796
300 javascript 로 stylesheet 컨트롤할때 키워드 매칭표 taknim 2009.01.22 6783
299 iframe 크기 자동조절 (네이버) - 파이어폭스가능 taknim 2009.01.22 7577
298 POST방식으로 다른 charset Action에 Data 넘겨주는 방법 taknim 2009.01.22 10247
297 mysql 자동 최적화 / 복구 스크립트 taknim 2009.01.16 9794
296 [KISA] mod security 매뉴얼 및 동영상 taknim 2009.01.14 10658
» 유용한 함수 pack, unpack, ip2long, long2ip taknim 2009.01.13 33346
294 윈도우 리부팅.끄기.잠금 아이콘 만들기 [1] taknim 2009.01.08 9601