nodejs_buffer.alloc

目录

    nodejs文档

    Buffer.alloc(size[, fill[, encoding]])

    • size 新 Buffer 的所需长度。
    • fill 用于预填充新 Buffer 的值。默认值: 0。
    • encoding 如果 fill 是一个字符串,则这是它的字符编码。默认值: 'utf8'。
    const buf1=Buffer.alloc(5);
    console.log(buf1);//<Buffer 00 00 00 00 00>
    
    const buf2=Buffer.alloc(5,"a");
    console.log(buf2);//<Buffer 61 61 61 61 61>
    
    const buf3=Buffer.alloc(11,"abcdefghijklmnopqrstuvw","UTF-8");
    console.log(buf3);//<Buffer 61 62 63 64 65 66 67 68 69 6a 6b>
    
    原文地址:https://www.cnblogs.com/Syinho/p/12602040.html