%% 十进制转十六进制函数(包括正负整数及小数),并根据设置显示大小端
function hex_x=f_dec2hex(x,N,flag) %x表示十进制数,N表示转换成的十六进制数的位数(字节数),flag表示大小端(1是小端,2是大端)
if x==round(x) %整数
if(x>=0)
hex_x=dec2hex(x,N);
else
hex_x=dec2hex(256^N+x,N);
end
else %小数
if N==4
hex_x=num2hex(single(x));
elseif N==8
hex_x=num2hex(x);
end
end
hex_x=sprintf(['%0',num2str(2*N),'s'],hex_x); %补充空位0
if flag==1
hex_x=upper(cell2mat(fliplr(regexp(hex_x,'\w\w','match')))); %小端排序
elseif flag==2
hex_x=upper(hex_x); %大端排序
end
end
- 1
- 2
- 3
前往页