buffer to byte array

6,626 views
Skip to first unread message

Justin Cormack

unread,
Nov 29, 2010, 11:33:54 AM11/29/10
to nod...@googlegroups.com

Is there a function to convert a node Buffer to a byte array? You can
initialize one from an array, but not the opposite apparently?

Justin

Isaac Schlueter

unread,
Nov 29, 2010, 12:13:54 PM11/29/10
to nod...@googlegroups.com
Here you go:

Buffer.prototype.toByteArray = function () { return
Array.prototype.slice.call(this, 0) }

--i

> --
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en.
>
>

Isaac Schlueter

unread,
Nov 29, 2010, 12:15:09 PM11/29/10
to nod...@googlegroups.com
Attack of the line break!

Should be all on one line, or like this:

Buffer.prototype.toByteArray = function () {
return Array.prototype.slice.call(this, 0)
}

Nathan Rajlich

unread,
Nov 29, 2010, 1:20:51 PM11/29/10
to nod...@googlegroups.com
Though, why would you need this :P

Justin Cormack

unread,
Nov 29, 2010, 1:40:47 PM11/29/10
to nod...@googlegroups.com

Thanks of course I should have realized that!

As to the reason, I end up with an array of buffers as some of the
data arrives in multiple events from the original stream, and
sometimes it is easier to join them all up into a single byte array. I
could avoid doing it, and do some of the time...

Justin

Reply all
Reply to author
Forward
0 new messages