MATLAB: Datahex = sprintf(‘%02x’, T1)?

charactersprintf

T1 =00111001 10001010 01011001 1011010010101100000000000100100011010001010110011110001001101010111100110111101111
datahex =
30303131313030312031303030313031302030313031313030312031303131303130303130313031313030303030303030303030313030313030303131303130303031303130313130303131313130303031303031313031303130313131313030313130313131313031313131
T1=[T1 count]
datahex = sprintf('%02x', T1)
I don't know why output in that format],T1 is character

Best Answer

First, if there are spaces in your T1 string, get rid of them:
T1(T1 == ' ') = []; %remove spaces
For demo purpose:
T1 = char(randi(double(['0' '1']), 1, 128)); %random demo string of 128 '0' and '1'
Then, assuming that the length of T1 is a multiple of 8:
datahex = num2cell(lower(dec2hex(bin2dec(reshape(T1, [], 8)))), 2)'
Basically, reshape your string into columns of 8 characters. Convert each row from binary string to decimal number and then from decimal number to hexadecimal string. Optionally, convert to lowercase and cell array.