Hex encoding

From Nethereum/Nethereum · View interactive version

Hex encoding is an AI agent skill from the GitHub repository Nethereum/Nethereum. Convert between bytes, strings, and hex in Nethereum. Use when the user needs hex conversion, byte array to hex, hex to bytes, HexBigInteger, hex prefix handling, or UTF-8 hex encoding.

How to use this skill

Skill file

Hex Encoding with Nethereum

NuGet: Nethereum.Hex (included in Nethereum.Web3)

Bytes to hex

var data = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF };

var hexWithPrefix = data.ToHex(prefix: true);    // "0xdeadbeef"
var hexWithoutPrefix = data.ToHex(prefix: false); // "deadbeef"

Source: HexConversionDocExampleTests.ShouldConvertBytesToHex

Hex to bytes

var hex = "0xdeadbeef";
var bytes = hex.HexToByteArray();
// { 0xDE, 0xAD, 0xBE, 0xEF }

Source: HexConversionDocExampleTests.ShouldConvertHexToBytes

Prefix handling

// Ensure prefix is present (idempotent)
"deadbeef".EnsureHexPrefix();   // "0xdeadbeef"
"0xdeadbeef".EnsureHexPrefix(); // "0xdeadbeef"

// Check prefix
"0xdeadbeef".HasHexPrefix(); // true

// Remove prefix
"0xdeadbeef".RemoveHexPrefix(); // "deadbeef"

Source: HexConversionDocExampleTests.ShouldEnsureHexPrefix, ShouldCheckAndRemoveHexPrefix

Hex comparison (case-insensitive)

var hex1 = "0xDeAdBeEf";
var hex2 = "0xdeadbeef";

hex1.IsTheSameHex(hex2); // true

Source: HexConversionDocExampleTests.ShouldCompareHexStrings

HexBigInteger for gas and value

var fromNumber = new HexBigInteger(new BigInteger(1_000_000));
var fromHex = new HexBigInteger("0xf4240");

fromNumber.Value;    // BigInteger 1000000
fromNumber.HexValue; // "0xf4240"
fromHex.Value;       // BigInteger 1000000

Source: HexConversionDocExampleTests.ShouldCreateHexBigIntegerFromBothFormats

UTF-8 encoding

var text = "Hello Ethereum";
var hex = text.ToHexUTF8();
var decoded = hex.HexToUTF8String();
// decoded == "Hello Ethereum"

Source: HexConversionDocExampleTests.ShouldEncodeDecodeUtf8AsHex

Required usings

using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Hex.HexTypes;
using System.Numerics;
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.