javascript - Why Buffer copy prints me some garbage value? -
i new node.js,
i reading on node.js buffers.
i stumbled on buffer.copy method.
wrote code follows,
var bufferone = new buffer("this china"); var buffertwo = new buffer(20), starttarget = 0, sourcestart = 8, sourceend = 0; var bufferlength = bufferone.length; sourceend = bufferlength; console.log("buffer length "+bufferlength); bufferone.copy(buffertwo,starttarget,sourcestart,sourceend); console.log("buffer1 "+bufferone.tostring()); console.log("buffer2 "+buffertwo.tostring());
the output follows,
buffer length 13 buffer1 china buffer2 china
but prints garbage output after "china", follows,
buffer length 13 buffer1 china buffer2 china���*�
sometimes,
buffer length 13 buffer1 china buffer2 china���
can please tell me whats going on ?
the length of buffertwo
20, you're copying 13 bytes. remaining 7 bytes contain semi-random data.
documented here:
passing number first argument
buffer()
(e.g.new buffer(10)
), allocates newbuffer
object of specified size. memory allocated suchbuffer
instances not initialized , can contain sensitive data.
Comments
Post a Comment