Function Repository Resource:

# NibbleArray

An array of nibbles (four-bit values)

Contributed by: Daniel Sanchez (Wolfram Research)
 ResourceFunction["NibbleArray"][{n1,n2,…}] constructs a NibbleArray object containing the nibble values ni. ResourceFunction["NibbleArray"]["string"] constructs a NibbleArray object by extracting nibble values from a Base16-encoded string.

## Details and Options

When displayed in InputForm, a NibbleArray object displays as ResourceFunction["NibbleArray"]["string"], where "string" encodes the nibbles in Base16 encoding.
In ResourceFunction["NibbleArray"][{n1,n2,}], all the nibble values ni should be integers between 0 and 15.
Normal[ResourceFunction["NibbleArray"][]] yields the list of nibble values in the nibble array.
Functions such as Length and Equal work with NibbleArray objects.
Functions such as First, Last, Min and Max yield integer nibble values.
Part works on ResourceFunction["NibbleArray"] objects, yielding an integer nibble value when it returns a single element and a NibbleArray object when it returns multiple elements.
NibbleArray[ByteArray[]] converts a byte array to a NibbleArray object.

## Examples

### Basic Examples (2)

Create a NibbleArray from a list of nibble values:

 In[1]:=
 Out[1]=

Convert back to a list of nibble values:

 In[2]:=
 Out[2]=

Create a NibbleArray from a Base16-encoded string:

 In[3]:=
 Out[3]=
 In[4]:=
 Out[4]=

NibbleArray uses Base16 in InputForm:

 In[5]:=
 Out[5]=

### Scope (3)

Get the Length of a NibbleArray:

 In[6]:=
 Out[6]=

Extract parts of a NibbleArray:

 In[7]:=
 Out[7]=
 In[8]:=
 Out[8]=
 In[9]:=
 Out[9]=

Extract a sequence of nibbles:

 In[10]:=
 Out[10]=

Reverse a nibble array:

 In[11]:=
 Out[11]=
 In[12]:=
 Out[12]=

### Properties and Relations (2)

NibbleArray is a wrapper around a packed array:

 In[13]:=
 Out[13]=
 In[14]:=
 Out[14]=

Convert a ByteArray to a NibbleArray:

 In[15]:=
 In[16]:=
 Out[16]=
 In[17]:=
 Out[17]=
 In[18]:=
 Out[18]=

### Neat Examples (1)

In Ethereum's yellow paper, hex-prefix encoding is defined as an efficient method of encoding an arbitrary number of nibbles as a byte array:

 In[19]:=
 In[20]:=
 Out[20]=
 In[21]:=
 Out[21]=

### Possible Issues (1)

NibbleArray only accepts a List of integers between 0 and 15, or a Base16-encoded string:

 In[22]:=
 Out[22]=
 In[23]:=
 Out[23]=

Daniel Sanchez

## Version History

• 1.0.0 – 12 August 2020

## Author Notes

Internally, a NibbleArray is represented as a PackedArray.