zeroPad

Learn about the zeroPad utils method

Learn about the zeroPad utils method

Introduction

The zeroPad method is used to pad a hex string into a byte array with leading zeros so that its length is 64 characters.

Usage

This method should be used when you want to pad a hex string into a byte array with leading zeros so that its length is 64 characters. For example, you can use this method to pad the hex string '0x1234' into:

Byte Array
Uint8Array(64) [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 18, 52
]

Here’s a simple code example to demonstrate how to use this method:

Javascript
1const { Utils } = require("alchemy-sdk");
2
3let hexString = '0x1234';
4
5let zeroPaddedHexByteArray = Utils.zeroPad(hexString, 64);
6console.log(zeroPaddedHexByteArray);
7
8/*
9Output:
10
11Uint8Array(64) [
12 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17 0, 0, 18, 52
18]
19*/