Search code examples
arraysmatlabuint32

Saving byte-wise a uint_32 to a 1 by 4 array in MATLAB


I would like to save each byte of a uint_32 number to the elements of a 1x4 matrix using Matlab

a = zeros(1,4,'uint8');
val = uint32(2^32-1);

How can I split val byte-wise and insert each single element in the array?


Solution

  • From what I could understood, I think you are looking for something like this with the least significant byte going as the rightmost element in the output vector -

    bits = reshape(bitget(num,32:-1:1),8,[]); %// num is the input number
    weights2 = 2.^([7:-1:0]);
    out = weights2*bits