How do I create the coinbase from the genesis block?
The nBits don't exactly get swapped. The notation on the wiki is in big-endian notation, which is basically the notation you'd use when displaying it, similar how the number twelve is written 12 and not 21 (different number order) but can be stored as 21 by a computer.

Your modern computer uses little-endian, which means that if you write something like :

int myInt = 0x1d00ffff;


It will actually store that in the RAM as 0xff 0xff 0x00 0x1d

Because of that, when you take the pointer of that int, and convert it to a unsigned char*, you get the little-endian order of it. Which happens to be what the reference client does :

(const unsigned char*)pszTimestamp 


So, to put myInt in a char[], all you have to do is copy it there:

char tx[4];
int myInt = 0x1d00ffff;
memcpy(tx, &amp;myInt, 4);
// tx now contains 0xff 0xff 0x00 0x1d




The only things that bitcoin actually swaps are network port numbers, which are stored in the memory as big endian numbers. Anything else doesn't really get swapped by bitcoin, it just stores them in the CPU's native endianness.