MATLAB: How to separate first 8 digits in binary format and convert them to the hex

binarybinary to hexdigits separation

I need a code to ask me to enter a number in binary format, and then separate them byte by byte (each 8 digits) then displace low and high value bytes and convert them to the hex.
for example if the input is 1000000011000000 then the output would be c080

Best Answer

Enter the value as a string and then convert. First below does the endian swap directly; if want to do this way write a little helper function that does it.
Alternatively, use the builtin swapbytes function on the internal representation.
MATL
>> b='1000000011000000'
b =
1000000011000000
>> dec2hex(bin2dec([b(9:16) b(1:8)]))
ans =
C080
>> dec2hex(swapbytes(uint16(bin2dec(b))))
ans =
C080
>>