[DGD] Big Endian Conversion

Par Winzell par.winzell at alyx.com
Sat Sep 25 04:27:30 CEST 2010


On 9/24/10 9:09 PM, Neil McBride wrote:
> Perhaps. The new handshake is relatively simple, but it requires taking a 32-bit integer
> and expressing it as a big-endian 32-bit number which is an aspect I'm struggling with. Is
> there a simple way to go about that in DGD that I've not found in the documentation? For
> example, python and php both have the pack function which does this quite easilly.

You output four bytes for a 32-bit integer. In DGD you'd typically be constructing a 
string with the bytes in sequence. There's nothing very tricky about these bytes, they 
correspond directly to how DGD (and pretty much all other languages) store integers 
internally. You just have to output them in the right order, which in practice means some 
bit shifting operations.

Algorithmically, we're talking something like:

string intToBigEndian (int n)
{
     string str = "XXXX";
     str[0] = (n >> 24) & 0xFF;
     str[1] = (n >> 16) & 0xFF;
     str[2] = (n >>  8) & 0xFF;
     str[3] = (n >>  0) & 0xFF;

     return str;
}

with some obvious optimizations if you're thus inclined. Apologies, also, if I've 
forgotten precisely how LPC syntax works. :)

Zel



More information about the DGD mailing list