How do I encode a 4-byte string as a single 32-bit integer?

algorithm, c#, powershell, python

Solution

To 32-bit unsigned integer:

uint x = BitConverter.ToUInt32(Encoding.ASCII.GetBytes("isoy"), 0); // 2037347177

To string:

string s = Encoding.ASCII.GetString(BitConverter.GetBytes(x));      // "isoy"

BitConverter uses the native endianness of the machine.

Problem

First, a disclaimer. I'm not a CS grad nor a math major, so simplicity is important. I have a four-character string (e.g. "isoy") that I need to pass as a single 32-bit integer field. Of course at the other end, I need to decode it back to a string. The string will only contain A-Z, and case is not important, if that helps. The funny part is that I'm starting with PowerShell on the sending end and Linux at the receiving end. I can use Perl or Python there, with a preference for Python. I don't actually need answers in each language, I'm most interested in a PowerShell (C# also good) example for going both ways.

Original source