Converting long to big-endian 32 bit number

I'm attempting to implement a web socket server and the handshake requires me to obtain two numbers, which I have. But it says expressed as a big-endian 32 bit number. I'm assuming this to mean a BCD but not fully understanding this as a php implmentation of the same process uses pack('N', $number).

The exact specification says:

he concatenation of the number obtained from processing the |Sec-
WebSocket-Key1| field, expressed as a big-endian 32 bit number, the
number obtained from processing the |Sec-WebSocket-Key2| field, again
expressed as a big-endian 32 bit number, and finally the eight bytes
at the end of the handshake, form a 128 bit string whose MD5 sum is
then used by the server to prove that it read the handshake.

Well I have all the bits sorted apart from encoding these numbers (which are currently in long variables into whatever that statement says it needs.

I'm fine with the md5 and the other bits.

Can anybody cast any light on what I need to do to convert my existing long variable please.

Well I have all the bits sorted

What does this mean?
You have them sorted into some other bit ordering?

BCD != Big endian.
BCD is simply decimal digits in place of hex digits, so 20 decimal would be represented as 0x20.

The AVR is little-endian.

When I say

Well I have all the bits sorted

I meant I have all the other parts of the problem taken care of other than this issue.

I know BCD != Big endian but according to the PHP implementation they obtained the required number by using the stated line of code which transforms an integer into a binary coded decimal.

Please correct me if I'm wrong but I thought a binary encoded decimal was each digit of the number being converted expressed in binary i.e. 127 = 0001 0010 0111 and big endian/little endian was just the order of the bytes and does not depend of the architecture of the arduino.

The full code snippet to satisfy the bit of specification I quoted was:

 return md5(
            pack('N', $this->_doStuffToObtainAnInt32($key1)).
            pack('N', $this->_doStuffToObtainAnInt32($key2)).
            $code,
            true
        );

I've got my Int32 on the arduino I just need to find out how to do the equivalent of the pack() method on the arduino.

By convention data in most internet protocols is packed big-endian. This means that the byte of each field representing the biggest bit values is put first.

On most systems, you use ntohl (network to host long) and ntohs (network to host short) to convert 32bit and 16bit numbers from network byte order (big endian) to your host's byte order (little endian for Arduino/AVR).

Here's an implementation of ntohl():

void ntohl(uint32_t* value) {
uint32_t tmp_a = (*value & 0xff000000) >> 24;
uint32_t tmp_b = (*value & 0x00ff0000) >> 8;
uint32_t tmp_c = (*value & 0x0000ff00) << 8 ;
uint32_t tmp_d = (*value & 0x000000ff) << 24;
*value = tmp_d | tmp_c |tmp_b | tmp_a;
}

Going the other way, use htonl() and htons().

(http://codesearch.google.com/codesearch/p?hl=en#QeTTHWGt2sU/trunk/avr/net/util.c&q=ntohl%20avr&sa=N&cd=1&ct=rc)

You should use htonl and htons to convert numbers from "host" to "network" (i.e. big-endian) format. They're both in sockutil.h in the Ethernet library. htonl will convert 32-bit numbers and htons does the same for 16-bit.

There's also ntohl and ntohs to map things coming the other way (although IIRC they're basically synonyms).