Function Repository Resource:

NibbleArray

Source Notebook

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]:=
ResourceFunction["NibbleArray"][{1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1}]
Out[1]=

Convert back to a list of nibble values:

In[2]:=
Normal[%]
Out[2]=

Create a NibbleArray from a Base16-encoded string:

In[3]:=
ResourceFunction["NibbleArray"]["10ffff"]
Out[3]=
In[4]:=
Normal[%]
Out[4]=

NibbleArray uses Base16 in InputForm:

In[5]:=
ResourceFunction["NibbleArray"][%] // InputForm
Out[5]=

Scope (3) 

Get the Length of a NibbleArray:

In[6]:=
Length[ResourceFunction["NibbleArray"][Range[15]]]
Out[6]=

Extract parts of a NibbleArray:

In[7]:=
n = ResourceFunction["NibbleArray"][Range[15]]
Out[7]=
In[8]:=
First[n]
Out[8]=
In[9]:=
Last[n]
Out[9]=

Extract a sequence of nibbles:

In[10]:=
n[[5 ;; 10]]
Out[10]=

Reverse a nibble array:

In[11]:=
Reverse@ResourceFunction["NibbleArray"][Range[5]]
Out[11]=
In[12]:=
Normal[%]
Out[12]=

Properties and Relations (2) 

NibbleArray is a wrapper around a packed array:

In[13]:=
ResourceFunction["NibbleArray"][RandomInteger[15, 10000]]
Out[13]=
In[14]:=
Normal[%] // Developer`PackedArrayQ
Out[14]=

Convert a ByteArray to a NibbleArray:

In[15]:=
ba = ByteArray[Range[32]];
In[16]:=
na = ResourceFunction["NibbleArray"][ba]
Out[16]=
In[17]:=
Normal[na]
Out[17]=
In[18]:=
InputForm@ResourceFunction["NibbleArray"][ByteArray["AQIDBAUGBwg="]]
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]:=
HP[x_NibbleArray, t_] := If[EvenQ[Length@x],
   ByteArray[
    Join[{16*f[t]}, (16*#1 + #2) & @@@ Partition[Normal[x], 2, 2]]]
   ,
   ByteArray[
    Join[{16*(f[t] + 1) + First[Normal[x]]}, (16*#1 + #2) & @@@ Partition[Rest[Normal[x]], 2, 2]]]
   ];
f[t_] := If[t =!= 0, 2, 0];
In[20]:=
HP[ResourceFunction["NibbleArray"]["fa36"], 1]
Out[20]=
In[21]:=
Table[{i, #, IntegerDigits[#[[1]], 2, 8]} &@
  HP[ResourceFunction["NibbleArray"]["ffa36"], i], {i, {0, 1}}]
Out[21]=

Possible Issues (1) 

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

In[22]:=
ResourceFunction["NibbleArray"][Range[32]]
Out[22]=
In[23]:=
ResourceFunction["NibbleArray"]["aaGh"]
Out[23]=

Publisher

Daniel Sanchez

Version History

  • 1.0.0 – 12 August 2020

Author Notes

Internally, a NibbleArray is represented as a PackedArray.

License Information