Understanding hello world example

153 views
Skip to first unread message

Dave

unread,
Jul 21, 2010, 11:53:38 AM7/21/10
to BSON
Hi Guys,
Excuse me if this question is a lame one.
I'm trying to write a client server app in perl for
which I would like to use bson as the communication protocol.

I was trying to understand the hello world example posted on the
homepage, but didn't quite get it.
Can someone explain why we start with 0x16
for the hello world example but the 2nd example
starts with a 0x1.

Gracias
Dave

Zane Shelby

unread,
Aug 9, 2010, 6:12:02 AM8/9/10
to BSON
Hi Dave,

We don't start with 0x1, we start with '1'. Here's why:

From the grammar, the first 4 bytes of a document are going to be a 32
bit integer representing the number of bytes in the document. The
first example is 22 bytes long, which is 16 in hex. So far so good.
The second example is a bit more subtle. The document in the second
example is 49 bytes long. You'd think, then, that the second example
would begin with \x31, since 49 is 31 in hex. Turns out it actually
does. 49 is also the character code for the character '1' in UTF-8
(and ASCII), so when the encoding is represented as a byte string (as
in the example) it's _rendered_ as '1'.

Make sense?
Reply all
Reply to author
Forward
0 new messages