4byte uint8 array to single uint32 value
108 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Maximilian Becker
il 7 Dic 2020
Commentato: Steven Lord
il 25 Mag 2023
I have an uint8 array containing 4 elements each representing 1byte.
myArray = [0x32 0x45 0x56 0x81] % just an example
% looks like this: myArray(1)=8bit, myArray(2)=8bit, myArray(3)=8bit, myArray(4)=8bit]
I mainly need to extract all the 32 bits and combine them to one uint32 value:
myValue = 0x32455681
%should look like this 32bit: myArray(1)myArray(2)myArray(3)myArray(4)
The goal is basically to remove the array structure and chain all the 32bits together
I once came from such an uint32 number and used the rearrange and bitget function to crate this array structure, but I somehow don't see how to come back.
As an important requirement, all the used functions must be able to be generate to C code
Any ideas?
0 Commenti
Risposta accettata
Ameer Hamza
il 7 Dic 2020
Modificato: Ameer Hamza
il 7 Dic 2020
You can use typecast()
myArray = [0x32 0x45 0x56 0x81];
out = swapbytes(typecast(myArray, 'uint32')); % swapbytes is needed on little-endian systems
Result
>> out
out =
uint32
843404929
>> dec2hex(out)
ans =
'32455681'
7 Commenti
Steven Lord
il 25 Mag 2023
format longg
x = randi([intmin('uint32') intmax('uint32')], 1)
y = typecast(x, 'uint8')
format hex
x
y
swapbytes(x)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!