I like it better when I can leverage open source code. Cross-pollination of ideas from other language communities can also provide interesting design ideas.
I found the following that I may use instead of writing my own parser
Read the blog post about it from its author.
Provides a neat syntax for parsing binary data, derived from Erlang’s bit syntax. I see examples for Node.js, I wonder if it works within the browser.
If you are looking for something similar for Ruby, you can take a look at BinData.