# Interpret hexadecimal sequence as a binary matrix that can be used in AdjacencyGraph

I have a database of hex sequences stored as blob.
Each of them is a binary matrix representing the adjacency matrix of a directed graph.
I want to use the AdjacencyGraph to plot the graphs.
It takes a binary matrix as parameter but the format is like {{0,1},{1,0}}.
I can definitely write a small piece of code to do the translation.
Just in case I'm reinventing the wheel, is there already a Mathematica function that handles this?

For example: 00000000000000C80000000000000040000000000000006800000000000000580000000000000008000000000000004500000000000000430000000000000001

then (8 bytes each row)
00000000000000C8
0000000000000040
0000000000000068
0000000000000058
0000000000000008
0000000000000045
0000000000000043
0000000000000001

C8
40
68
58
08
45
43
01

then (into binary format)
10101000
01000000
01101000
01011000
00001000
01000101
01000011
00000001

then (into Mathematica)

AdjacencyGraph[{{1, 0, 1, 0, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 0, 1, 0, 0, 0},
{0, 1, 0, 1, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 1, 0, 1},
{0, 1, 0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 1}}, DirectedEdges -> True]


then the output

I doubt there is a builtin function that goes from a string to an adjacency matrix. It can be done in short (and combineable) steps as below.

In[1]:= hexstr =
"00000000000000C8000000000000004000000000000000680000000000000058000\
0000000000008000000000000004500000000000000430000000000000001";

In[3]:= hexsubstrs = StringPartition[hexstr, 16]

(* Out[3]= {"00000000000000C8", "0000000000000040", "0000000000000068", \
"0000000000000058", "0000000000000008", "0000000000000045", \
"0000000000000043", "0000000000000001"} *)

In[6]:= hexvals = Map[FromDigits[#, 16] &, hexsubstrs]

(* Out[6]= {200, 64, 104, 88, 8, 69, 67, 1} *)

In[9]:= adjmat = Map[IntegerDigits[#, 2, 8] &, hexvals]

(* Out[9]= {{1, 1, 0, 0, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 1,
0, 1, 0, 0, 0}, {0, 1, 0, 1, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0,
0}, {0, 1, 0, 0, 0, 1, 0, 1}, {0, 1, 0, 0, 0, 0, 1, 1}, {0, 0, 0, 0,
0, 0, 0, 1}} *)


I would not be surprised if there is a way to contract a couple of those steps into one (beyond the obvious way of nesting the code).

• I ended up writing a C code to do the work (used C to read data from database anyways). But it's good to know the functions in your solution (I haven't used most of them). Thank you Daniel.
– qunb
Nov 29, 2015 at 18:22