Nethereum.Hex 5.8.0

Prefix Reserved
dotnet add package Nethereum.Hex --version 5.8.0
                    
NuGet\Install-Package Nethereum.Hex -Version 5.8.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nethereum.Hex" Version="5.8.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nethereum.Hex" Version="5.8.0" />
                    
Directory.Packages.props
<PackageReference Include="Nethereum.Hex" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nethereum.Hex --version 5.8.0
                    
#r "nuget: Nethereum.Hex, 5.8.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Nethereum.Hex@5.8.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nethereum.Hex&version=5.8.0
                    
Install as a Cake Addin
#tool nuget:?package=Nethereum.Hex&version=5.8.0
                    
Install as a Cake Tool

Nethereum.Hex

Hexadecimal encoding and decoding utilities for Ethereum-specific types including String, BigInteger, and byte arrays.

Overview

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.

Key Features

  • HexBigInteger: Type-safe wrapper for BigInteger with automatic hex encoding/decoding
  • Hex Byte Conversions: Extension methods for converting between byte arrays and hex strings
  • Ethereum Standards Compliance: Follows Ethereum JSON-RPC hex encoding specifications
  • JSON Serialization: Built-in support for both Newtonsoft.Json and System.Text.Json
  • Cross-Platform: Supports .NET Framework, .NET Core, .NET 5+, Unity, and Xamarin

Installation

dotnet add package Nethereum.Hex

Dependencies

None. This package has zero dependencies.

JSON Serialization Support:

  • Includes converters for Newtonsoft.Json (if available in your application)
  • Includes converters for System.Text.Json (.NET 6.0+, if available in your application)
  • These are peer dependencies - not included by this package

Key Concepts

HexBigInteger

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:

  • Prefix with 0x
  • Use most compact representation (no leading zeros)
  • Exception: zero is represented as 0x0
  • Big-endian byte ordering

Hex Byte Extensions

Extension methods for working with hex strings and byte arrays:

  • ToHex() - Convert byte array to hex string
  • HexToByteArray() - Convert hex string to byte array
  • EnsureHexPrefix() - Add 0x prefix if missing
  • RemoveHexPrefix() - Strip 0x prefix if present
  • IsHex() - Validate hex string format

Hex BigInteger Extensions

Extension methods for BigInteger hex conversion:

  • ToHex() - Convert BigInteger to hex string
  • HexToBigInteger() - Convert hex string to BigInteger
  • Support for both little-endian and big-endian byte ordering

Quick Start

using 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"

Usage Examples

Example 1: Working with Wei Amounts

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

Example 2: Hex String Conversions

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

Example 3: Compact Encoding (Ethereum Standard)

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

Example 4: BigInteger to Hex Conversion with Extensions

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);

Example 5: Equality Comparisons

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

Example 6: JSON Serialization

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);

API Reference

Core Types

HexBigInteger

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; }
}

Extension Methods

HexByteConvertorExtensions
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);
}
HexBigIntegerConvertorExtensions
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);
}

Used By (Consumers)

Almost all Nethereum packages depend on Nethereum.Hex as it provides fundamental encoding:

  • Nethereum.ABI - ABI encoding/decoding requires hex conversions
  • Nethereum.RPC - JSON-RPC uses hex encoding for all numeric values
  • Nethereum.Util - Utility functions build on hex primitives
  • Nethereum.Signer - Signature components use hex encoding
  • Nethereum.Contracts - Contract interactions require hex encoding
  • Nethereum.Web3 - Main facade uses hex types throughout

Dependencies

  • Newtonsoft.Json - JSON serialization support
  • System.Text.Json (.NET 6.0+) - Modern JSON serialization
  • Nethereum.BigInteger.N351 (embedded) - BigInteger implementation for legacy frameworks

Additional Resources

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (51)

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

GitHub repositories (3)

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
5.8.0 7,471 1/6/2026
5.0.0 355,565 5/28/2025
4.29.0 397,056 2/10/2025
4.28.0 69,989 1/7/2025
4.27.1 14,858 12/24/2024
4.27.0 2,830 12/24/2024
4.26.0 166,349 10/1/2024
4.25.0 26,917 9/19/2024
4.21.4 110,222 8/9/2024
4.21.3 20,966 7/22/2024
4.21.2 104,496 6/26/2024
4.21.1 3,633 6/26/2024
4.21.0 15,728 6/18/2024
4.20.0 397,020 3/28/2024
4.19.0 106,266 2/16/2024
4.18.0 327,957 11/21/2023
4.17.1 187,582 9/28/2023
4.17.0 19,352 9/27/2023
4.16.0 129,616 8/14/2023
4.15.2 137,443 7/11/2023
4.15.1 6,361 7/11/2023
4.15.0 6,939 7/11/2023
4.14.0 260,145 3/19/2023
4.13.0 151,303 2/18/2023
4.12.0 279,879 12/9/2022
4.11.0 266,072 10/27/2022
4.9.0 266,163 9/27/2022
4.8.0 240,503 8/24/2022
4.7.0 224,281 7/20/2022
4.6.1 156,965 6/18/2022
4.6.0 15,034 6/16/2022
4.5.0 521,795 5/13/2022
4.4.1 129,462 4/27/2022
4.4.0 18,676 4/27/2022
4.3.0 95,084 4/12/2022
4.2.0 201,706 2/18/2022
4.1.1 628,308 11/4/2021
4.1.0 36,437 10/15/2021
4.0.5 162,984 8/12/2021
4.0.4 11,475 8/10/2021
4.0.3 29,720 8/8/2021
4.0.2 10,648 8/5/2021
4.0.1 18,174 7/28/2021
4.0.0 24,053 7/26/2021
3.8.0 466,819 7/3/2020
3.7.1 154,029 2/13/2020
3.7.0 13,205 2/13/2020
3.6.0 37,518 1/27/2020
3.5.0 48,103 12/31/2019
3.4.0 198,692 7/29/2019
3.3.0 129,371 4/23/2019
3.2.0 77,396 4/8/2019
3.1.2 28,187 3/13/2019
3.1.1 8,936 3/12/2019
3.1.0 30,231 3/12/2019
3.0.0 193,681 11/28/2018
3.0.0-rc3 7,727 10/25/2018
3.0.0-rc2 5,506 10/24/2018
3.0.0-rc1 11,944 7/25/2018
2.5.1 195,693 6/5/2018
2.5.0 8,854 6/4/2018
2.4.0 156,201 3/11/2018
2.3.1 10,844 3/7/2018
2.3.0 9,148 3/6/2018
2.2.3 24,561 12/16/2017
2.2.2 4,757 12/16/2017
2.2.0 12,998 12/8/2017
2.1.0 16,245 10/23/2017
2.0.1 8,792 10/4/2017
2.0.0 15,946 9/26/2017
2.0.0-rc7 6,358 8/17/2017
2.0.0-rc6-2 5,894 7/29/2017
2.0.0-rc6.1 1,240 7/26/2017
2.0.0-rc5 6,015 6/19/2017
2.0.0-rc4 6,004 6/6/2017
2.0.0-rc3 5,594 4/11/2017
2.0.0-rc2-fix 5,837 4/6/2017
2.0.0-rc2 1,908 4/5/2017
2.0.0-rc1 5,666 2/8/2017
1.0.6 7,057 2/3/2017
1.0.5 3,384 1/31/2017
1.0.4 7,518 12/10/2016
1.0.3 4,860 11/28/2016
1.0.2 4,196 11/21/2016
1.0.1 4,526 10/31/2016
1.0.0 15,412 9/14/2016
1.0.0-rc6 3,240 9/12/2016
1.0.0-rc5 6,080 8/1/2016
1.0.0-rc4 3,868 7/29/2016
1.0.0-rc1 2,645 3/30/2016
1.0.0-alpha 2,973 2/27/2016