BitSharper.Utils.Uint32ToByteArrayBe C# (CSharp) Method

Uint32ToByteArrayBe() public static method

public static Uint32ToByteArrayBe ( uint val, byte @out, int offset ) : void
val uint
@out byte
offset int
return void
        public static void Uint32ToByteArrayBe(uint val, byte[] @out, int offset)
        {
            @out[offset + 0] = (byte) (val >> 24);
            @out[offset + 1] = (byte) (val >> 16);
            @out[offset + 2] = (byte) (val >> 8);
            @out[offset + 3] = (byte) (val >> 0);
        }

Usage Example

        /// <summary>
        /// Writes message to to the output stream.
        /// </summary>
        /// <exception cref="IOException"/>
        public void Serialize(Message message, Stream @out)
        {
            string name;

            if (!_names.TryGetValue(message.GetType(), out name))
            {
                throw new Exception("BitcoinSerializer doesn't currently know how to serialize " + message.GetType());
            }

            var header = new byte[4 + _commandLen + 4 + (_usesChecksumming ? 4 : 0)];

            Utils.Uint32ToByteArrayBe(_params.PacketMagic, header, 0);

            // The header array is initialized to zero so we don't have to worry about
            // NULL terminating the string here.
            for (var i = 0; i < name.Length && i < _commandLen; i++)
            {
                header[4 + i] = (byte)name[i];
            }

            var payload = message.BitcoinSerialize();

            Utils.Uint32ToByteArrayLe((uint)payload.Length, header, 4 + _commandLen);

            if (_usesChecksumming)
            {
                var hash = Utils.DoubleDigest(payload);
                Array.Copy(hash, 0, header, 4 + _commandLen + 4, 4);
            }

            @out.Write(header);
            @out.Write(payload);

            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Sending {0} message: {1}", name, Utils.BytesToHexString(header) + Utils.BytesToHexString(payload));
            }
        }