[Node.js] 공부 4 일차 -Node.js Buffer 사용법
개발환경
- OS : windows
- 개발툴 : Brackets
- Node.js 버전 : 6.11.1 LTS
참조 Node.js API : https://nodejs.org/dist/latest-v6.x/docs/api/buffer.html
Buffer API 확인하기 |
Buffer.alloc(size[, fill[, encoding]]) |
- size <integer> : Buffer 사이즈 지정
- fill <string> | <Buffer> | <integer> : Buffer 초기화 시 초기 설정 값 지정 Default : 0
- encoding <string> : Buffer 초기화 시 encoding 설정 Default: 'utf8'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var buf_01 = Buffer.alloc(10); // hex Prints: <Buffer 00 00 00 00 00 00 00 00 00 00> console.log("buf_01 init : " + buf_01); console.log("buf_01 init(hex) : " + buf_01.toString("hex")); console.log("buf_01 init size : " + buf_01.length); var buf_02 = Buffer.alloc(10, 1); // hex Prints: <Buffer 01 01 01 01 01 01 01 01 01 01> console.log("buf_02 init : " + buf_02); console.log("buf_02 init(hex) : " + buf_02.toString("hex")); console.log("buf_02 init size : " + buf_02.length); var buf_03 = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64'); // hex Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64> console.log("buf_03 init : " + buf_03); console.log("buf_03 init(hex) : " + buf_03.toString("hex")); console.log("buf_03 init size : " + buf_03.length); | cs |
결과 화면
1 2 3 4 5 6 7 8 9 | buf_01 init : buf_01 init(hex) : 00000000000000000000 buf_01 init size : 10 buf_02 init : buf_02 init(hex) : 01010101010101010101 buf_02 init size : 10 buf_03 init : hello world buf_03 init(hex) : 68656c6c6f20776f726c64 buf_03 init size : 11 | cs |
buf.toString([encoding[, start[, end]]]) |
- encoding <string> : 해당 값의 인코딩 방식 설정 Default: 'utf8'
- start <integer> : 인코딩 시작 위치 Default: 0
- end <integer> : 인코딩 종료 위치 Default: buf.length
- Returns: <string>
1 2 3 4 5 6 7 8 9 10 11 | var buf_04 = Buffer.alloc(26); for (let i = 0; i < 26; i++) { buf_04[i] = i + 97; } console.log("buf_04 ascii print : " + buf_04.toString("ascii")); console.log("buf_04 ascii print[0-10] : " + buf_04.toString('ascii', 0, 10)); console.log("buf_04 hex print : " + buf_04.toString("hex")); console.log("buf_04 utf8 print : " + buf_04.toString("utf8")); console.log("buf_04 binary print : " + buf_04.toString("binary")); | cs |
결과화면
1 2 3 4 5 | buf_04 ascii print : abcdefghijklmnopqrstuvwxyz buf_04 ascii print[0-10] : abcdefghij buf_04 hex print : 6162636465666768696a6b6c6d6e6f707172737475767778797a buf_04 utf8 print : abcdefghijklmnopqrstuvwxyz buf_04 binary print : abcdefghijklmnopqrstuvwxyz | cs |
Buffer.byteLength(string[, encoding]) |
- string <string> | <Buffer> | <TypedArray> | <DataView> | <ArrayBuffer> : 바이트 길이를 확인할 변수 입력 Default encoding : 'utf8'
- Returns: <integer> : 입력된 변수의 바이트 길이를 반환한다.
1 2 3 4 5 6 7 8 9 10 11 | var buf_05 = Buffer("테스트입니다."); console.log("buf_05 init : " + buf_05); console.log("buf_05 init size : " + buf_05.length); console.log("buf_05 init byte size : " + Buffer.byteLength(buf_05)); var str_01 = '\u00bd + \u00bc = \u00be'; console.log("str_01 init : " + str_01); console.log("str_01 init size : " + str_01.length); console.log("str_01 init byte size : " + Buffer.byteLength(str_01)); | cs |
결과화면
1 2 3 4 5 6 | buf_05 init : 테스트입니다. buf_05 init size : 19 buf_05 init byte size : 19 str_01 init : ½ + ¼ = ¾ str_01 init size : 9 str_01 init byte size : 12 | cs |
buf.fill(value[, offset[, end]][, encoding]) |
- value <string> | <Buffer> | <integer> : 해당 변수에 채워넣을 값
- offset <integer> : 시작 위치 Default: 0
- end <integer> : 종료 위치. Default: buf.length
- encoding <string> : 인코딩 설정 Default: 'utf8'
- Returns: <Buffer> : 입력된 값으로 설정된 값 반환한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var buf_06 = Buffer.alloc(10, 1); console.log("buf_06 init : " + buf_06); console.log("buf_06 init(hex) : " + buf_06.toString("hex")); console.log("buf_06 init size : " + buf_06.length); buf_06.fill(0x02); console.log("buf_06 fill first init : " + buf_06); console.log("buf_06 fill first init(hex) : " + buf_06.toString("hex")); console.log("buf_06 fill first init size : " + buf_06.length); buf_06.fill("A"); console.log("buf_06 fill second init : " + buf_06); console.log("buf_06 fill second init(hex) : " + buf_06.toString("hex")); console.log("buf_06 fill second init size : " + buf_06.length); | cs |
결과화면
1 2 3 4 5 6 7 8 9 | buf_06 init : buf_06 init(hex) : 01010101010101010101 buf_06 init size : 10 buf_06 fill first init : buf_06 fill first init(hex) : 02020202020202020202 buf_06 fill first init size : 10 buf_06 fill second init : AAAAAAAAAA buf_06 fill second init(hex) : 41414141414141414141 buf_06 fill second init size : 10 | cs |
buf.writeInt32BE(value, offset[, noAssert]) : Big-Endian, buf.writeInt32LE(value, offset[, noAssert]) : Little-Endian |
- value <integer> : Buffer에 쓰일 숫자 변수
- offset <integer> : 해당 변수가 저장될 시작위치
- noAssert <boolean> : 사용된 예를 확인하지 못함 생략해도 사용하는데 지장 없음 Default: false
- Returns: <integer> buffer 변수에 해당 변수가 쓰여진 마지막 위치를 반환
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | var num = Number(100); var buf_07 = Buffer.alloc(4); var len_07 = buf_07.writeInt32BE(num, 0); console.log("buf_07 init : " + buf_07); console.log("buf_07.writeInt32BE(num, 0)(hex) : " + buf_07.toString("hex")); console.log("buf_07.writeInt32BE(num, 0) size : " + buf_07.length); console.log("len_07 : " + len_07); var buf_08 = Buffer.alloc(4); var len_08 = buf_08.writeInt32LE(num, 0); console.log("buf_08 init : " + buf_08); console.log("buf_08.writeInt32LE(num, 0)(hex) : " + buf_08.toString("hex")); console.log("buf_08.writeInt32LE(num, 0) size : " + buf_08.length); console.log("len_08 : " + len_08); var buf_09 = Buffer.alloc(10); var len_09 = buf_09.writeInt32LE(num, 2); console.log("buf_09 init : " + buf_09); console.log("buf_09.writeInt32LE(num, 2)(hex) : " + buf_09.toString("hex")); console.log("buf_09.writeInt32LE(num, 2) size : " + buf_09.length); console.log("len_09 : " + len_09); | cs |
결과화면
1 2 3 4 5 6 7 8 9 10 11 12 | buf_07 init : d buf_07.writeInt32BE(num, 0)(hex) : 00000064 buf_07.writeInt32BE(num, 0) size : 4 len_07 : 4 buf_08 init : d buf_08.writeInt32LE(num, 0)(hex) : 64000000 buf_08.writeInt32LE(num, 0) size : 4 len_08 : 4 buf_09 init : d buf_09.writeInt32LE(num, 2)(hex) : 00006400000000000000 buf_09.writeInt32LE(num, 2) size : 10 len_09 : 6 | cs |
buf.slice([start[, end]]) |
- start <integer> : 시작 위치 Default: 0
- end <integer> : 종료 위치 Default: buf.length
- Returns: <Buffer> : 해당 위치로 잘려진 값을 반환한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | var buf_10 = Buffer.alloc(26); for (let i = 0; i < 26; i++) { buf_10[i] = i + 97; } console.log("buf_10 utf8 print : " + buf_10.toString("utf8")); console.log("buf_10 hex print : " + buf_10.toString("hex")); var buf_11 = Buffer.alloc(5); buf_11 = buf_10.slice(0, 5); console.log("buf_11 utf8 print : " + buf_11.toString("utf8")); console.log("buf_11 hex print : " + buf_11.toString("hex")); var buf_12 = Buffer.alloc(5); buf_12 = buf_10.slice(4, 6); console.log("buf_12 utf8 print : " + buf_12.toString("utf8")); console.log("buf_12 hex print : " + buf_12.toString("hex")); | cs |
결과화면
1 2 3 4 5 6 | buf_10 utf8 print : abcdefghijklmnopqrstuvwxyz buf_10 hex print : 6162636465666768696a6b6c6d6e6f707172737475767778797a buf_11 utf8 print : abcde buf_11 hex print : 6162636465 buf_12 utf8 print : ef buf_12 hex print : 6566 | cs |
Buffer.concat(list[, totalLength]) |
- list <Array> : 합쳐질 Buffer변수의 배열
- totalLength <integer> : 합쳐질 변수의 전체 길이
- Returns: <Buffer> : 합쳐진 값을 반환한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var buf_13 = Buffer("hello"); var buf_14 = Buffer(" world"); var buf_15 = Buffer(" 환영합니다."); var totalLength = buf_13.length + buf_14.length + buf_15.length; console.log("buf_13 utf8 print : " + buf_13.toString("utf8")); console.log("buf_14 utf8 print : " + buf_14.toString("utf8")); console.log("buf_15 utf8 print : " + buf_15.toString("utf8")); console.log("buf_13.length + buf_14.length + buf_15.length = " + totalLength); var buf_16 = Buffer.concat([buf_13, buf_14, buf_15], totalLength); console.log("buf_16 utf8 print : " + buf_16.toString("utf8")); console.log("buf_16 hex print : " + buf_16.toString("hex")); console.log("buf_16 length : " + buf_16.length); | cs |
결과화면
1 2 3 4 5 6 7 | buf_13 utf8 print : hello buf_14 utf8 print : world buf_15 utf8 print : 환영합니다. buf_13.length + buf_14.length + buf_15.length = 28 buf_16 utf8 print : hello world 환영합니다. buf_16 hex print : 68656c6c6f20776f726c6420ed9998ec9881ed95a9eb8b88eb8ba42e buf_16 length : 28 | cs |
'Node.js' 카테고리의 다른 글
[Node.js] 공부 3 일차 - TCP 서버 및 클라이언트 (0) | 2017.08.13 |
---|---|
[Node.js] 공부 2 일차 - HTTP 서버 소스 분석 (3) | 2017.08.06 |
[Node.js] 공부 1 일차 - Node.js 설치 및 동작 테스트 (0) | 2017.07.31 |