public uint256(byte[] vch, int offset, int length, bool lendian = true) { if (length != WIDTH_BYTE) { throw new FormatException("the byte array should be 32 bytes long"); } #if HAS_SPAN if (BitConverter.IsLittleEndian && lendian) { var uints = MemoryMarshal.Cast <byte, ulong>(vch.AsSpan().Slice(offset, length)); pn0 = uints[0]; pn1 = uints[1]; pn2 = uints[2]; pn3 = uints[3]; return; } #endif if (!lendian) { if (length != vch.Length) { vch = vch.Take(32).ToArray(); } vch = vch.Reverse().ToArray(); } pn0 = Utils.ToUInt64(vch, offset + 8 * 0, true); pn1 = Utils.ToUInt64(vch, offset + 8 * 1, true); pn2 = Utils.ToUInt64(vch, offset + 8 * 2, true); pn3 = Utils.ToUInt64(vch, offset + 8 * 3, true); }
public uint256(ReadOnlySpan <byte> bytes) { if (bytes.Length != WIDTH_BYTE) { throw new FormatException("the byte array should be 32 bytes long"); } if (BitConverter.IsLittleEndian) { var uints = MemoryMarshal.Cast <byte, ulong>(bytes); pn0 = uints[0]; pn1 = uints[1]; pn2 = uints[2]; pn3 = uints[3]; return; } pn0 = Utils.ToUInt64(bytes.Slice(0), true); pn1 = Utils.ToUInt64(bytes.Slice(8 * 1), true); pn2 = Utils.ToUInt64(bytes.Slice(8 * 2), true); pn3 = Utils.ToUInt64(bytes.Slice(8 * 3), true); }
/// <summary> /// Create a uint256 from a string in big endian /// </summary> /// <param name="str"></param> public uint256(string str) { if (str == null) { throw new ArgumentNullException(nameof(str)); } if (str.Length != 64) { if (str.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { str = str.Substring(2); } str = str.Trim(); if (str.Length != 64) { throw new FormatException("A uint256 must be 64 characters"); } } #if HAS_SPAN if (BitConverter.IsLittleEndian) { Span <byte> tmp = stackalloc byte[32]; Encoder.DecodeData(str, tmp); tmp.Reverse(); Span <ulong> uints = MemoryMarshal.Cast <byte, ulong>(tmp); pn0 = uints[0]; pn1 = uints[1]; pn2 = uints[2]; pn3 = uints[3]; return; } #endif var bytes = Encoder.DecodeData(str); Array.Reverse(bytes); pn0 = Utils.ToUInt64(bytes, 8 * 0, true); pn1 = Utils.ToUInt64(bytes, 8 * 1, true); pn2 = Utils.ToUInt64(bytes, 8 * 2, true); pn3 = Utils.ToUInt64(bytes, 8 * 3, true); }