![]() |
VOOZH | about |
dotnet add package Nethereum.Hex --version 6.1.0
NuGet\Install-Package Nethereum.Hex -Version 6.1.0
<PackageReference Include="Nethereum.Hex" Version="6.1.0" />
<PackageVersion Include="Nethereum.Hex" Version="6.1.0" />Directory.Packages.props
<PackageReference Include="Nethereum.Hex" />Project file
paket add Nethereum.Hex --version 6.1.0
#r "nuget: Nethereum.Hex, 6.1.0"
#:package Nethereum.Hex@6.1.0
#addin nuget:?package=Nethereum.Hex&version=6.1.0Install as a Cake Addin
#tool nuget:?package=Nethereum.Hex&version=6.1.0Install as a Cake Tool
Hexadecimal encoding and decoding utilities for Ethereum-specific types including String, BigInteger, and byte arrays.
Nethereum.Hex provides foundational hexadecimal conversion capabilities specifically designed for Ethereum development. It handles the encoding and decoding of values according to Ethereum's hex encoding standards, including proper 0x prefixing, compact representation (no leading zeros except for zero itself), and big-endian byte ordering.
dotnet add package Nethereum.Hex
None. This package has zero dependencies.
JSON Serialization Support:
HexBigInteger is a type-safe wrapper around System.Numerics.BigInteger that automatically handles hex encoding and decoding. It's commonly used for representing Ethereum quantities like wei amounts, block numbers, gas values, and nonces.
Ethereum Hex Encoding Rules:
0x0x0Extension methods for working with hex strings and byte arrays:
ToHex() - Convert byte array to hex stringHexToByteArray() - Convert hex string to byte arrayEnsureHexPrefix() - Add 0x prefix if missingRemoveHexPrefix() - Strip 0x prefix if presentIsHex() - Validate hex string formatExtension methods for BigInteger hex conversion:
ToHex() - Convert BigInteger to hex stringHexToBigInteger() - Convert hex string to BigIntegerusing Nethereum.Hex.HexTypes;
using System.Numerics;
// Create from BigInteger
var amount = new HexBigInteger(1000000000000000000); // 1 ETH in wei
Console.WriteLine(amount.HexValue); // "0xde0b6b3a7640000"
// Create from hex string
var blockNumber = new HexBigInteger("0x400");
Console.WriteLine(blockNumber.Value); // 1024
// Access both representations
var gas = new HexBigInteger(21000);
Console.WriteLine($"Decimal: {gas.Value}"); // 21000
Console.WriteLine($"Hex: {gas.HexValue}"); // "0x5208"
using Nethereum.Hex.HexTypes;
using System.Numerics;
// Encoding wei amounts for transactions
var oneEther = BigInteger.Parse("1000000000000000000");
var encoded = new HexBigInteger(oneEther);
Assert.Equal("0xde0b6b3a7640000", encoded.HexValue);
// Decoding wei amounts from JSON-RPC responses
var hexValue = "0x8ac7230489e80000";
var decoded = new HexBigInteger(hexValue);
Assert.Equal("10000000000000000000", decoded.Value.ToString()); // 10 ETH
using Nethereum.Hex.HexConvertors.Extensions;
// Convert byte array to hex
byte[] data = new byte[] { 0x12, 0x34, 0x56, 0x78 };
string hex = data.ToHex(prefix: true);
// Result: "0x12345678"
// Convert hex to byte array
string hexString = "0xabcdef";
byte[] bytes = hexString.HexToByteArray();
// Result: [0xab, 0xcd, 0xef]
// Ensure proper formatting
string withoutPrefix = "ff00aa";
string formatted = withoutPrefix.EnsureHexPrefix();
// Result: "0xff00aa"
// Validate hex strings
bool isValid = "0x1234abcd".IsHex(); // true
bool isInvalid = "0xghij".IsHex(); // false
using Nethereum.Hex.HexTypes;
using System.Numerics;
// Ethereum requires compact encoding (no leading zeros)
var value = new HexBigInteger(new BigInteger(1024));
Assert.Equal("0x400", value.HexValue); // NOT "0x0400"
// Zero is always represented as "0x0"
var zero = new HexBigInteger(new BigInteger(0));
Assert.Equal("0x0", zero.HexValue);
// Decoding handles both compact and padded formats
var compact = new HexBigInteger("0x400");
var padded = new HexBigInteger("0x0400");
Assert.Equal(compact.Value, padded.Value); // Both equal 1024
using Nethereum.Hex.HexConvertors.Extensions;
using System.Numerics;
// Convert BigInteger to hex (big-endian, compact)
BigInteger value = 1000000;
string hex = value.ToHex(littleEndian: false, compact: true);
// Result: "0xf4240"
// Convert hex to BigInteger (big-endian)
string hexString = "0xde0b6b3a7640000";
BigInteger result = hexString.HexToBigInteger(isHexLittleEndian: false);
// Result: 1000000000000000000
// Convert to byte array with endianness control
byte[] bytes = value.ToByteArray(littleEndian: false);
using Nethereum.Hex.HexTypes;
// HexBigInteger supports value equality
var val1 = new HexBigInteger(100);
var val2 = new HexBigInteger(100);
Assert.True(val1 == val2);
Assert.True(val1.Equals(val2));
// Different values are not equal
var val3 = new HexBigInteger(101);
Assert.False(val1 == val3);
// Can compare values created from different sources
var fromInt = new HexBigInteger(256);
var fromHex = new HexBigInteger("0x100");
Assert.True(fromInt == fromHex); // Both represent 256
using Nethereum.Hex.HexTypes;
using Newtonsoft.Json;
using System.Numerics;
// Automatic JSON serialization with Newtonsoft.Json
public class Transaction
{
public HexBigInteger Value { get; set; }
public HexBigInteger GasPrice { get; set; }
}
var tx = new Transaction
{
Value = new HexBigInteger(1000000000000000000),
GasPrice = new HexBigInteger(20000000000)
};
string json = JsonConvert.SerializeObject(tx);
// Result: {"Value":"0xde0b6b3a7640000","GasPrice":"0x4a817c800"}
// Deserialization works automatically
var deserialized = JsonConvert.DeserializeObject<Transaction>(json);
Assert.Equal(1000000000000000000, deserialized.Value.Value);
Ethereum-compliant hex-encoded BigInteger wrapper.
public class HexBigInteger : HexRPCType<BigInteger>
{
public HexBigInteger(string hex);
public HexBigInteger(BigInteger value);
public BigInteger Value { get; set; }
public string HexValue { get; set; }
}
public static class HexByteConvertorExtensions
{
// Byte array conversions
public static string ToHex(this byte[] value, bool prefix = false);
public static byte[] HexToByteArray(this string value);
public static string ToHexCompact(this byte[] value);
// Prefix handling
public static bool HasHexPrefix(this string value);
public static string EnsureHexPrefix(this string value);
public static string RemoveHexPrefix(this string value);
// Validation
public static bool IsHex(this string value);
public static bool IsTheSameHex(this string first, string second);
}
public static class HexBigIntegerConvertorExtensions
{
// BigInteger to hex
public static string ToHex(this BigInteger value, bool littleEndian, bool compact = true);
public static byte[] ToByteArray(this BigInteger value, bool littleEndian);
// Hex to BigInteger
public static BigInteger HexToBigInteger(this string hex, bool isHexLittleEndian);
// HexBigInteger helpers
public static BigInteger? GetValue(this HexBigInteger hexBigInteger);
}
Almost all Nethereum packages depend on Nethereum.Hex as it provides fundamental encoding:
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net451 net451 is compatible. net452 net452 was computed. net46 net46 was computed. net461 net461 is compatible. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on Nethereum.Hex:
| Package | Downloads |
|---|---|
|
Nethereum.ABI
Encoding and decoding of ABI Types, functions, events of Ethereum contracts |
|
|
Nethereum.RLP
RLP Encoding and decoding |
|
|
Nethereum.Util
Nethereum Utilities library, including Sha3 Keccack encoding and Address Checksum |
|
|
Nethereum.Signer
Nethereum signer library to sign and verify messages, RLP and transactions using an Ethereum account |
|
|
Nethereum.JsonRpc.Client
Nethereum JsonRpc.Client core library to use in conjunction with either the JsonRpc.RpcClient, the JsonRpc.IpcClient or other custom Rpc provider |
Showing the top 3 popular GitHub repositories that depend on Nethereum.Hex:
| Repository | Stars |
|---|---|
|
ChainSafe/web3.unity
🕹 Unity SDK for building games that interact with blockchains.
|
|
|
yc-l/yc.boilerplate
YC. Boilerplate is a set of loose coupling, flexible combination, complete functions, convenient development, and reduces the workload of development.
|
|
|
biheBlockChain/MyLinkToken
开源链克口袋,玩客币钱包
|
| Version | Downloads | Last Updated |
|---|---|---|
| 6.1.0 | 92,993 | 3/25/2026 |
| 6.0.4 | 15,768 | 3/18/2026 |
| 6.0.3 | 2,708 | 3/18/2026 |
| 6.0.1 | 4,224 | 3/17/2026 |
| 6.0.0 | 5,201 | 3/16/2026 |
| 5.8.0 | 126,764 | 1/6/2026 |
| 5.0.0 | 480,982 | 5/28/2025 |
| 4.29.0 | 506,512 | 2/10/2025 |
| 4.28.0 | 88,144 | 1/7/2025 |
| 4.27.1 | 16,472 | 12/24/2024 |
| 4.27.0 | 9,362 | 12/24/2024 |
| 4.26.0 | 184,433 | 10/1/2024 |
| 4.25.0 | 38,164 | 9/19/2024 |
| 4.21.4 | 152,462 | 8/9/2024 |
| 4.21.3 | 23,444 | 7/22/2024 |
| 4.21.2 | 113,716 | 6/26/2024 |
| 4.21.1 | 4,882 | 6/26/2024 |
| 4.21.0 | 24,494 | 6/18/2024 |
| 4.20.0 | 469,842 | 3/28/2024 |
| 4.19.0 | 132,032 | 2/16/2024 |