You can just use a standard 2D array to represent your data (or rather, an array of arrays.) If your data is coming from a SQL table like in the first example then you'll need to convert that serial one-dimensional data into your 5x3 matrix.
From there it's fairly straight forward. You just need to create a new 2D array with flipped row/column dimension sizes, then map over the data to the corresponding cell. In matrix terms this is called transposing. Here's a snippet to demonstrate:
https://pastebin.com/yDJTmAKg
(had to pastebin because of too many characters)
Result:
<?xml version="1.0" encoding="utf-16"?>
<Result
bin0="0000 0000 0000"
bin1="0000 0000 0000"
bin2="0000 0000 FFFF"
bin3="FFFF 0000 0000"
bin4="0000 0000 0000">
<ResultConvert
bin0="0000 0000 0000 FFFF 0000"
bin1="0000 0000 0000 0000 0000"
bin2="0000 0000 FFFF 0000 0000" />
</Result>