ethers.js/docs/v5/api/utils/bytes/index.html
2021-02-08 15:26:10 -05:00

158 lines
32 KiB
HTML

<!DOCTYPE html>
<html class="paged">
<head>
<title>Byte Manipulation</title>
<link rel="stylesheet" type="text/css" href="/v5/static/style.css">
<meta property="og:title" content="Byte Manipulation"/>
<meta property="og:description" content="Documentation for ethers, a complete, tiny and simple Ethereum library."/>
<meta property="og:image" content="/v5/static/social.jpg"/>
</head>
<body>
<div class="sidebar">
<div class="header">
<div class="logo"><a href="/v5/"><div class="image"></div><div class="name">ethers</div><div class="version">v5.0</div></a></div>
<div class="search"><form action="/v5/search/" method="GET"><input name="search" id="search" /></form><span class="search-icon">&#9906;</span></div>
</div>
<div class="toc"><div>
<div class="link title"><a href="/v5/">Documentation</a></div><div class="base show link depth-1"><a href="/v5/getting-started/">Getting Started</a></div><div class="base show link depth-1"><a href="/v5/concepts/">Ethereum Basics</a></div><div class="hide link depth-2"><a href="/v5/concepts/events/">Events</a></div><div class="hide link depth-2"><a href="/v5/concepts/gas/">Gas</a></div><div class="hide link depth-2"><a href="/v5/concepts/security/">Security</a></div><div class="hide link depth-2"><a href="/v5/concepts/best-practices/">Best Practices</a></div><div class="base show link depth-1"><a href="/v5/api-keys/">Provider API Keys</a></div><div class="base ancestor show link depth-1"><a href="/v5/api/">Application Programming Interface</a></div><div class="show link depth-2"><a href="/v5/api/providers/">Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/provider/">Provider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/api-providers/">API Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/other/">Other Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/types/">Types</a></div><div class="show link depth-2"><a href="/v5/api/signer/">Signers</a></div><div class="show link depth-2"><a href="/v5/api/contract/">Contract Interaction</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract/">Contract</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract-factory/">ContractFactory</a></div><div class="hide link depth-3"><a href="/v5/api/contract/example/">Example: ERC-20 Contract</a></div><div class="ancestor show link depth-2"><a href="/v5/api/utils/">Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/abi/">Application Binary Interface</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/coder/">AbiCoder</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/formats/">ABI Formats</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/fragments/">Fragments</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/interface/">Interface</a></div><div class="show link depth-3"><a href="/v5/api/utils/address/">Addresses</a></div><div class="show link depth-3"><a href="/v5/api/utils/bignumber/">BigNumber</a></div><div class="myself ancestor ancestor show link depth-3"><a href="/v5/api/utils/bytes/">Byte Manipulation</a></div><div class="link show child depth-4"><a href="#byte-manipulation--types">Types</a></div><div class="link show child depth-4"><a href="#byte-manipulation--inspection">Inspection</a></div><div class="link show child depth-4"><a href="#byte-manipulation--converting-between-arrays-and-hexstrings">Converting between Arrays and Hexstrings</a></div><div class="link show child depth-4"><a href="#byte-manipulation--array-manipulation">Array Manipulation</a></div><div class="link show child depth-4"><a href="#byte-manipulation--hexstring-manipulation">Hexstring Manipulation</a></div><div class="link show child depth-4"><a href="#byte-manipulation--signature-conversion">Signature Conversion</a></div><div class="link show child depth-4"><a href="#byte-manipulation--random-bytes">Random Bytes</a></div><div class="show link depth-3"><a href="/v5/api/utils/constants/">Constants</a></div><div class="show link depth-3"><a href="/v5/api/utils/display-logic/">Display Logic and Input</a></div><div class="show link depth-3"><a href="/v5/api/utils/encoding/">Encoding Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/fixednumber/">FixedNumber</a></div><div class="show link depth-3"><a href="/v5/api/utils/hashing/">Hashing Algorithms</a></div><div class="show link depth-3"><a href="/v5/api/utils/hdnode/">HD Wallet</a></div><div class="show link depth-3"><a href="/v5/api/utils/logger/">Logging</a></div><div class="show link depth-3"><a href="/v5/api/utils/properties/">Property Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/signing-key/">Signing Key</a></div><div class="show link depth-3"><a href="/v5/api/utils/strings/">Strings</a></div><div class="show link depth-3"><a href="/v5/api/utils/transactions/">Transactions</a></div><div class="show link depth-3"><a href="/v5/api/utils/web/">Web Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/wordlists/">Wordlists</a></div><div class="show link depth-2"><a href="/v5/api/other/">Other Libraries</a></div><div class="hide link depth-3"><a href="/v5/api/other/assembly/">Assembly</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/dialect/">Ethers ASM Dialect</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/api/">Utilities</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/ast/">Abstract Syntax Tree</a></div><div class="hide link depth-3"><a href="/v5/api/other/hardware/">Hardware Wallets</a></div><div class="show link depth-2"><a href="/v5/api/experimental/">Experimental</a></div><div class="base show link depth-1"><a href="/v5/cli/">Command Line Interfaces</a></div><div class="hide link depth-2"><a href="/v5/cli/ethers/">Sandbox Utility</a></div><div class="hide link depth-2"><a href="/v5/cli/asm/">Assembler</a></div><div class="hide link depth-2"><a href="/v5/cli/ens/">Ethereum Naming Service</a></div><div class="hide link depth-2"><a href="/v5/cli/typescript/">TypeScript</a></div><div class="hide link depth-2"><a href="/v5/cli/plugin/">Making Your Own</a></div><div class="base show link depth-1"><a href="/v5/cookbook/">Cookbook</a></div><div class="hide link depth-2"><a href="/v5/cookbook/react-native/">React Native (and ilk)</a></div><div class="base show link depth-1"><a href="/v5/migration/">Migration Guide</a></div><div class="hide link depth-2"><a href="/v5/migration/web3/">Migration: From Web3.js</a></div><div class="hide link depth-2"><a href="/v5/migration/ethers-v4/">Migration: From Ethers v4</a></div><div class="base show link depth-1"><a href="/v5/testing/">Testing</a></div><div class="base show link depth-1"><a href="/v5/contributing/">Contributing and Hacking</a></div><div class="base show link depth-1"><a href="/v5/other-resources/">Other Resources</a></div><div class="base show link depth-1"><a href="/v5/documentation/">Flatworm Docs</a></div><div class="base show link depth-1"><a href="/v5/license/">License and Copyright</a></div>
</div></div>
<div class="footer">
<a href="/v5/single-page/">Single Page</a>
</div>
</div>
<div class="content">
<div class="breadcrumbs"><a href="/v5/">Documentation</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/api/">API</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/api/utils/">Utilities</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<span class="current">Byte Manipulation</span></div>
<a name="byte-manipulation"></a><h1 class="show-anchors"><div>Byte Manipulation<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#byte-manipulation"></a></div></div></h1><p>While there are many high-level APIs for interacting with Ethereum, such as <a href="/v5/api/contract/contract/">Contracts</a> and <a href="/v5/api/providers/provider/">Providers</a>, a lot of the low level access requires byte manipulation operations.</p>
<p>Many of these operations are used internally, but can also be used to help normalize binary data representations from the output of various functions and methods.</p>
<a name="byte-manipulation--types"></a><h2 class="show-anchors"><div>Types<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#byte-manipulation--types"></a></div></div></h2>
<a name="Bytes"></a><a name="byte-manipulation--types--Bytes"></a><h3 class="show-anchors"><div>Bytes<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#Bytes"></a></div></div></h3><p>A <b>Bytes</b> is any object which is an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">TypedArray</a> with each value in the valid byte range (i.e. between 0 and 255 inclusive), or is an Object with a <code class="inline">length</code> property where each indexed property is in the valid byte range.</p>
<a name="BytesLike"></a><a name="byte-manipulation--types--BytesLike"></a><h3 class="show-anchors"><div>BytesLike<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#BytesLike"></a></div></div></h3><p>A <b>BytesLike</b> can be either a <a href="/v5/api/utils/bytes/#Bytes">Bytes</a> or a <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>.</p>
<a name="DataHexString"></a><a name="byte-manipulation--types--DataHexString"></a><h3 class="show-anchors"><div>DataHexString<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#DataHexString"></a></div></div></h3><p>A <b>DataHexstring</b> is identical to a <a href="/v5/api/utils/bytes/#HexString">HexString</a> except that it has an even number of nibbles, and therefore is a valid representation of binary data as a string.</p>
<a name="HexString"></a><a name="byte-manipulation--types--HexString"></a><h3 class="show-anchors"><div>HexString<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#HexString"></a></div></div></h3><p>A <b>Hexstring</b> is a string which has a <code class="inline">0x</code> prefix followed by any number of nibbles (i.e. case-insensitive hexadecimal characters, <code class="inline">0-9</code> and <code class="inline">a-f</code>).</p>
<a name="Signature"></a><a name="byte-manipulation--types--Signature"></a><h3 class="show-anchors"><div>Signature<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#Signature"></a></div></div></h3><p><ul><li><b>r</b> and <b>s</b> --- The x co-ordinate of <b>r</b> and the <b>s</b> value of the signature </li><li><b>v</b> --- The parity of the y co-ordinate of <b>r</b> </li><li><b>_vs</b> --- The <a href="https://eips.ethereum.org/EIPS/eip-2098">compact representation</a> of the <b>s</b> and <b>v</b> </li><li><b>recoveryParam</b> --- The normalized (i.e. 0 or 1) value of <b>v</b> </li></ul></p>
<a name="signature-raw"></a><a name="byte-manipulation--types--signature-raw"></a><h3 class="show-anchors"><div>Raw Signature<span class="inherits"> inherits string&lt;<a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt;65&gt;&gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#signature-raw"></a></div></div></h3><p>A <b>Raw Signature</b> is a common Signature format where the r, s and v are concatenated into a 65 byte (130 nibble) <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>.</p>
<a name="SignatureLike"></a><a name="byte-manipulation--types--SignatureLike"></a><h3 class="show-anchors"><div>SignatureLike<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#SignatureLike"></a></div></div></h3><p>A <b>SignatureLike</b> is similar to a <a href="/v5/api/utils/bytes/#Signature">Signature</a>, except redundant properties may be omitted or it may be a <a href="/v5/api/utils/bytes/#signature-raw">Raw Signature</a>.</p>
<p>For example, if <b>_vs</b> is specified, <b>s</b> and <b>v</b> may be omitted. Likewise, if <b>recoveryParam</b> is provided, <b>v</b> may be omitted (as in these cases the missing values can be computed).</p>
<a name="byte-manipulation--inspection"></a><h2 class="show-anchors"><div>Inspection<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#byte-manipulation--inspection"></a></div></div></h2>
<a name="utils-isBytes"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">isBytes</span><span class="symbol">(</span> <span class="param">object</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">boolean</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-isBytes"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L73">source</a></div></div><div class="body"><p>Returns true if and only if <i>object</i> is a valid <a href="/v5/api/utils/bytes/#Bytes">Bytes</a>.</p>
</div></div><a name="utils-isBytesLike"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">isBytesLike</span><span class="symbol">(</span> <span class="param">object</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">boolean</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-isBytesLike"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L69">source</a></div></div><div class="body"><p>Returns true if and only if <i>object</i> is a <a href="/v5/api/utils/bytes/#Bytes">Bytes</a> or <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>.</p>
</div></div><a name="utils-isHexString"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">isHexString</span><span class="symbol">(</span> <span class="param">object</span> <span class="symbol">,</span> <span class="symbol">[</span> <span class="param">length</span> <span class="symbol">]</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">boolean</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-isHexString"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L183">source</a></div></div><div class="body"><p>Returns true if and only if <i>object</i> is a valid hex string. If <i>length</i> is specified and <i>object</i> is not a valid <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> of <i>length</i> bytes, an InvalidArgument error is thrown.</p>
</div></div><a name="byte-manipulation--converting-between-arrays-and-hexstrings"></a><h2 class="show-anchors"><div>Converting between Arrays and Hexstrings<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#byte-manipulation--converting-between-arrays-and-hexstrings"></a></div></div></h2>
<a name="utils-arrayify"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">arrayify</span><span class="symbol">(</span> <span class="param">DataHexStringOrArrayish</span> <span class="symbol">[</span> <span class="symbol">,</span> <span class="param">options</span> <span class="symbol">]</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-arrayify"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L90">source</a></div></div><div class="body"><p>Converts <i>DataHexStringOrArrayish</i> to a Uint8Array.</p>
</div></div><a name="utils-hexlify"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">hexlify</span><span class="symbol">(</span> <span class="param">hexstringOrArrayish</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-hexlify"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L193">source</a></div></div><div class="body"><p>Converts <i>hexstringOrArrayish</i> to a <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>.</p>
</div></div><a name="utils-hexValue"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">hexValue</span><span class="symbol">(</span> <span class="param">aBigNumberish</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#HexString">HexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-hexValue"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L286">source</a></div></div><div class="body"><p>Converts <i>aBigNumberish</i> to a <a href="/v5/api/utils/bytes/#HexString">HexString</a>, with no <u>unnecessary</u> leading zeros.</p>
</div></div><div class="code-title"><div>Examples</div></div><div class="code"><span class="comment">// Convert a hexstring to a Uint8Array
</span>arrayify("0x1234")
<span class="result ok">// Uint8Array [ 18, 52 ]
</span>
<span class="comment">// Convert an Array to a hexstring
</span>hexlify([1, 2, 3, 4])
<span class="result ok">// '0x01020304'
</span>
<span class="comment">// Convert an Object to a hexstring
</span>hexlify({ length: 2, "0": 1, "1": 2 })
<span class="result ok">// '0x0102'
</span>
<span class="comment">// Convert an Array to a hexstring
</span>hexlify([ 1 ])
<span class="result ok">// '0x01'
</span>
<span class="comment">// Convert a number to a stripped hex value
</span>hexValue(1)
<span class="result ok">// '0x1'
</span>
<span class="comment">// Convert an Array to a stripped hex value
</span>hexValue([ 1, 2 ])
<span class="result ok">// '0x102'
</span></div><a name="byte-manipulation--array-manipulation"></a><h2 class="show-anchors"><div>Array Manipulation<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#byte-manipulation--array-manipulation"></a></div></div></h2>
<a name="utils-concat"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">concat</span><span class="symbol">(</span> <span class="param">arrayOfBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-concat"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L139">source</a></div></div><div class="body"><p>Concatenates all the <a href="/v5/api/utils/bytes/#BytesLike">BytesLike</a> in <i>arrayOfBytesLike</i> into a single Uint8Array.</p>
</div></div><a name="utils-stripZeros"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">stripZeros</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-stripZeros"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L153">source</a></div></div><div class="body"><p>Returns a Uint8Array with all leading <code class="inline">0</code> bytes of <i>aBtyesLike</i> removed.</p>
</div></div><a name="utils-zeroPad"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">zeroPad</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">,</span> <span class="param">length</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-zeroPad"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L170">source</a></div></div><div class="body"><p>Returns a Uint8Array of the data in <i>aBytesLike</i> with <code class="inline">0</code> bytes prepended to <i>length</i> bytes long.</p>
<p>If <i>aBytesLike</i> is already longer than <i>length</i> bytes long, an InvalidArgument error will be thrown.</p>
</div></div><a name="byte-manipulation--hexstring-manipulation"></a><h2 class="show-anchors"><div>Hexstring Manipulation<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#byte-manipulation--hexstring-manipulation"></a></div></div></h2>
<a name="utils-hexConcat"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">hexConcat</span><span class="symbol">(</span> <span class="param">arrayOfBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-hexConcat"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L278">source</a></div></div><div class="body"><p>Concatenates all the <a href="/v5/api/utils/bytes/#BytesLike">BytesLike</a> in <i>arrayOfBytesLike</i> into a single <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a></p>
</div></div><a name="utils-hexDataLength"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">hexDataLength</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-hexDataLength"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L252">source</a></div></div><div class="body"><p>Returns the length (in bytes) of <i>aBytesLike</i>.</p>
</div></div><a name="utils-hexDataSlice"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">hexDataSlice</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">,</span> <span class="param">offset</span> <span class="symbol">[</span> <span class="symbol">,</span> <span class="param">endOffset</span> <span class="symbol">]</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-hexDataSlice"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L262">source</a></div></div><div class="body"><p>Returns a <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> representation of a slice of <i>aBytesLike</i>, from <i>offset</i> (in bytes) to <i>endOffset</i> (in bytes). If <i>endOffset</i> is omitted, the length of <i>aBytesLike</i> is used.</p>
</div></div><a name="utils-hexStripZeros"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">hexStripZeros</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#HexString">HexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-hexStripZeros"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L292">source</a></div></div><div class="body"><p>Returns a <a href="/v5/api/utils/bytes/#HexString">HexString</a> representation of <i>aBytesLike</i> with all leading zeros removed.</p>
</div></div><a name="utils-hexZeroPad"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">hexZeroPad</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">,</span> <span class="param">length</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-hexZeroPad"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L304">source</a></div></div><div class="body"><p>Returns a <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> representation of <i>aBytesLike</i> padded to <i>length</i> bytes.</p>
<p>If <i>aBytesLike</i> is already longer than <i>length</i> bytes long, an InvalidArgument error will be thrown.</p>
</div></div><a name="byte-manipulation--signature-conversion"></a><h2 class="show-anchors"><div>Signature Conversion<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#byte-manipulation--signature-conversion"></a></div></div></h2>
<a name="utils-joinSignature"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">joinSignature</span><span class="symbol">(</span> <span class="param">aSignatureLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#signature-raw">RawSignature</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-joinSignature"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L441">source</a></div></div><div class="body"><p>Return the raw-format of <i>aSignaturelike</i>, which is 65 bytes (130 nibbles) long, concatenating the <b>r</b>, <b>s</b> and (normalized) <b>v</b> of a Signature.</p>
</div></div><a name="utils-splitSignature"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">splitSignature</span><span class="symbol">(</span> <span class="param">aSignatureLikeOrBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns"><a href="/v5/api/utils/bytes/#Signature">Signature</a></span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-splitSignature"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L322">source</a></div></div><div class="body"><p>Return the full expanded-format of <i>aSignaturelike</i> or a raw-format <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>. Any missing properties will be computed.</p>
</div></div><a name="byte-manipulation--random-bytes"></a><h2 class="show-anchors"><div>Random Bytes<div class="anchors"><a class="self" href="/v5/api/utils/bytes/#byte-manipulation--random-bytes"></a></div></div></h2>
<a name="utils-randomBytes"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">randomBytes</span><span class="symbol">(</span> <span class="param">length</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-randomBytes"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/random/src.ts/random.ts#L5">source</a></div></div><div class="body"><p>Return a new Uint8Array of <i>length</i> random bytes.</p>
</div></div><a name="utils-shuffled"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">shuffled</span><span class="symbol">(</span> <span class="param">array</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">Array&lt; any &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/bytes/#utils-shuffled"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/random/src.ts/shuffle.ts#L3">source</a></div></div><div class="body"><p>Return a copy of <i>array</i> shuffled using <a href="https://en.wikipedia.org/wiki/Fisher-Yates_shuffle">Fisher-Yates Shuffle</a>.</p>
</div></div><div class="code-title"><div>Examples</div></div><div class="code">utils.randomBytes(8)
<span class="result ok">// Uint8Array [ 97, 223, 223, 186, 224, 0, 90, 28 ]
</span>
const data = [ 1, 2, 3, 4, 5, 6, 7 ];
<span class="comment">// Returns a new Array
</span>utils.shuffled(data);
<span class="result ok">// [
</span><span class="result ok">// 5,
</span><span class="result ok">// 3,
</span><span class="result ok">// 1,
</span><span class="result ok">// 4,
</span><span class="result ok">// 6,
</span><span class="result ok">// 7,
</span><span class="result ok">// 2
</span><span class="result ok">// ]
</span>
<span class="comment">// The Original is unscathed...
</span>data
<span class="result ok">// [
</span><span class="result ok">// 1,
</span><span class="result ok">// 2,
</span><span class="result ok">// 3,
</span><span class="result ok">// 4,
</span><span class="result ok">// 5,
</span><span class="result ok">// 6,
</span><span class="result ok">// 7
</span><span class="result ok">// ]
</span></div>
<div class="footer">
<div class="nav previous"><a href="/v5/api/utils/bignumber/"><span class="arrow">&larr;</span>BigNumber</a></div>
<div class="nav next"><a href="/v5/api/utils/constants/">Constants<span class="arrow">&rarr;</span></a></div>
</div>
<div class="copyright">The content of this site is licensed under the <a href="https://choosealicense.com/licenses/cc-by-4.0/">Creative Commons License</a>. Generated on February 8, 2021, 3:25pm.</div>
</div>
<script src="/v5/static/script.js" type="text/javascript"></script>
<!--EXTRASCRIPT-->
</body>
</html>