Collection of utility functions used in web3.js for Ethereum dApp development
—
Comprehensive conversion utilities for transforming data between hex, bytes, numbers, strings, and Ethereum units. These functions handle the various data format conversions needed when working with blockchain data.
Automatically converts any value to hex representation with optional type detection.
/**
* Auto-converts any value to hex representation
* @param value - Value to convert (Numbers, Bytes, Address, boolean, object)
* @param returnType - If true, returns both hex and detected type
* @returns Hex string or object with hex and type information
*/
function toHex(value: Numbers | Bytes | Address | boolean | object, returnType?: boolean): HexString | ValueTypes;Converts values to number or bigint representation.
/**
* Converts value to number or bigint representation
* @param value - Numbers type to convert
* @returns number for small values, bigint for large values
*/
function toNumber(value: Numbers): number | bigint;
/**
* Auto-converts value to bigint representation
* @param value - Any value to convert
* @returns bigint representation
*/
function toBigInt(value: unknown): bigint;Converts various types to boolean values.
/**
* Converts various types to boolean
* @param value - Value to convert to boolean
* @returns boolean representation
*/
function toBool(value: boolean | string | number | unknown): boolean;/**
* Converts bytes to Uint8Array representation
* @param data - Bytes data to convert
* @returns Uint8Array representation
*/
function bytesToUint8Array(data: Bytes): Uint8Array;
/**
* Converts byte array to hex string
* @param bytes - Bytes to convert
* @returns Hex string representation
*/
function bytesToHex(bytes: Bytes): HexString;/**
* Converts hex string to byte array
* @param bytes - Hex string to convert
* @returns Uint8Array representation
*/
function hexToBytes(bytes: HexString): Uint8Array;
/**
* Converts hex string to number representation
* @param value - Hex string to convert
* @returns number or bigint depending on size
*/
function hexToNumber(value: HexString): bigint | number;
/**
* Converts hex to decimal string representation
* @param data - Hex string to convert
* @returns Decimal string representation
*/
function hexToNumberString(data: HexString): string;/**
* Converts value to hex representation with optional padding
* @param value - Numbers type to convert
* @param hexstrict - If true, ensures proper hex formatting
* @returns Hex string representation
*/
function numberToHex(value: Numbers, hexstrict?: boolean): HexString;/**
* Converts UTF-8 string to hex
* @param str - UTF-8 string to convert
* @returns Hex string representation
*/
function utf8ToHex(str: string): HexString;
/**
* Converts UTF-8 string to bytes
* @param str - UTF-8 string to convert
* @returns Uint8Array representation
*/
function utf8ToBytes(str: string): Uint8Array;
/**
* Converts hex to UTF-8 string
* @param str - Hex string to convert
* @returns UTF-8 string representation
*/
function hexToUtf8(str: HexString): string;
/**
* Converts hex or Uint8Array to UTF-8 (overloaded)
* @param input - Hex string or Uint8Array to convert
* @returns UTF-8 string representation
*/
function toUtf8(input: HexString | Uint8Array): string;/**
* Converts ASCII string to hex
* @param str - ASCII string to convert
* @returns Hex string representation
*/
function asciiToHex(str: string): HexString;
/**
* Converts hex to ASCII string
* @param str - Hex string to convert
* @returns ASCII string representation
*/
function hexToAscii(str: HexString): string;/**
* Converts wei to other ether units
* @param number - Wei amount to convert
* @param unit - Target unit (ether, gwei, etc.) or unit multiplier
* @returns String representation of converted amount
*/
function fromWei(number: Numbers, unit: EtherUnits | number): string;
/**
* Converts ether units to wei
* @param number - Amount in specified unit to convert
* @param unit - Source unit (ether, gwei, etc.) or unit multiplier
* @returns String representation of wei amount
*/
function toWei(number: Numbers, unit: EtherUnits | number): string;/**
* Converts address to checksum format
* @param address - Address to convert
* @returns Checksummed address string
*/
function toChecksumAddress(address: Address): string;Many conversion functions have aliases for backwards compatibility:
// Hex/Number aliases
const toDecimal = hexToNumber;
const fromDecimal = numberToHex;
// UTF-8 aliases
const fromUtf8 = utf8ToHex;
const stringToHex = utf8ToHex;
const hexToString = hexToUtf8;
// ASCII aliases
const fromAscii = asciiToHex;
const toAscii = hexToAscii;import {
toHex, toNumber, fromWei, toWei,
utf8ToHex, hexToUtf8, toChecksumAddress
} from "web3-utils";
// General conversion
const hexValue = toHex(1234); // "0x4d2"
const hexBoolean = toHex(true); // "0x01"
const number = toNumber("0x4d2"); // 1234
// Wei/Ether conversion
const ethAmount = fromWei("1000000000000000000", "ether"); // "1"
const gweiAmount = fromWei("1000000000", "gwei"); // "1"
const weiAmount = toWei("1.5", "ether"); // "1500000000000000000"
// String conversion
const hexString = utf8ToHex("Hello World"); // "0x48656c6c6f20576f726c64"
const originalString = hexToUtf8(hexString); // "Hello World"
// Address formatting
const addr = "0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed";
const checksumAddr = toChecksumAddress(addr);
// "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAeD"// Ethereum unit mapping
const ethUnitMap: Record<string, bigint>;
// Union type of available ether units
type EtherUnits = keyof typeof ethUnitMap;
// "wei" | "kwei" | "mwei" | "gwei" | "szabo" | "finney" | "ether"Install with Tessl CLI
npx tessl i tessl/npm-web3-utils