So I like using Perl, if anyone hasn’t realized yet. Python seems awesome and all, but I haven’t had the time to start writing things in Python. I’m still at the stage when, if a tool needs to be put together, it needs to be done ASAP; I can’t delay trying to figure out Python particulars.
In any case, one of our metrics publishing systems is wrapped by a few different object classes I put together in Perl (follows a certain protocol created by another group, etc). Of the many data types supported by our metrics receivers, we can do things like 32-bit unsigned integers, single precision (32-bit) floating points, and a few others.
Now, when I would publish the 32-bit uint’s, it would work great- no issues. The receivers would expect a byte stream, so I would use pack() to send my values as binary. For example:
$val=29;
print $sock pack('N',$val);
Easy, whatever. However, when I would send values as a single precision float, it would totally be hosed on the other end. For example:
$val="30.0";
print $sock pack('f',$val);
On the other end, they would get some massive negative number. I don’t deal with this too much, things usually just work. Thankfully, my mind was jogged in regards to standards- there are lots of standards around byte order for integers (16 bit, 32 bit, 64 bit), but not as much for float; one architecture may use big-endian, and another architecture might use little-endian.
In my searches, I found that Intel stores floating points in little-endian byte order, and Sparc stores them in big-endian byte order. Sure enough, I was trying to publish from an Intel-based BSD host to a Sparc based Solaris host. Aha!
Luckily, pack() can *also* handle this for us! With a simple carat added to the templates section of pack(), I can easily pack the value in big-endian byte order and resolve our issue. I do that like so:
$val="30.0";
print $sock pack('f>',$val);
And, ta da! Worked like a charm. Was it difficult to get it working? No. But, it was fun to have to deal with something like this. On any given day, you don’t really worry about endian-ness; things just work. This was one of those days where I actually had to use the old noggin, and that was refreshing.




Hah! Nice hack.
Big vs Little endian is one of my pet peeves – it seems like such a simple thing to agree on!
I’m so old… (How old?) … So old, that we used to fix it with “dd”. We used that to fix ascii vs ebcdic too (Yeah, *that* old.)
-Jim
hahaha that’s awesome. yeah, i just came across that dd option yesterday actually – i was having some fun adventures doing something with it the past few days, and hit the point where it was time i actually looked at the options instead of the usual if, of, bs, and occasionally count. oooh much fun was had.