matlab实现十进制数与十六进制数互相转换

DSP中浮点数分为32bit单精度和16bit半精度两种表示方式,如果需要与十进制小数互相转换怎么办呢?

%%%%%%%%%%%%%%%%%%十进制与十六进制互转%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%十进制->半精度
ddata=[1.43225097656250;1.28771972656250;1.01745605468750;0.855834960937500;0.803833007812500;0.733276367187500;0.641479492187500;0.567016601562500];
for i = 1 : 8
    dec2pfp(ddata(i),0)
end

%十进制->单精度
ddata=[1.43225097656250;1.28771972656250;1.01745605468750;0.855834960937500;0.803833007812500;0.733276367187500;0.641479492187500;0.567016601562500];
for i = 1 : 8
    dec2pfp(ddata(i),1)
end

%单精度/半精度->十进制,注意输入必须是字符形式
hdata=["402ed800" "400c6800 " "3ff9b000" "3fe0f000" "3fcbf000" "3fbaf000" "3f92d000" "3f8ab000"];
for i = 1 : 8
    pfp2dec(hdata(i))
end

涉及到的两个转换的子函数dec2pfp,pfp2dec如下

function output = dec2pfp(input, pfptype)

if 0 == pfptype
    if input > 65504
        output = '7FFF';
    elseif input < -65504
        output = 'FFFF';
    elseif (input < (1/1024+1)/2^16) && (input >= 0)
        output = '0000';
    elseif (input > -1*(1/1024+1)/2^16) && (input <= 0)
        output = '8000';
    else
        pfp_sign = input<0;
        pfp_exp = floor(log2(abs(input))) + 16;
        pfp_man = round( (abs(input) ./ (2.^(pfp_exp - 16)) - 1) * 2^10);
        if(pfp_man >= 2^10)
            pfp_man = 2^10-1;
        end

        pfp_sign = dec2bin(pfp_sign, 1)
        pfp_exp = dec2bin(pfp_exp, 5)
        pfp_man = dec2bin(pfp_man, 10)
        [pfp_sign pfp_exp pfp_man]
        output = dec2hex(bin2dec([pfp_sign pfp_exp pfp_man]), 4)
    end
else
    if input > 18446735277616529408
        output = '7FFFFFF0';
    elseif input < -18446735277616529408
        output = 'FFFFFFF0';
    elseif (input < (1/(2^20)+1)/2^64) && (input > 0)
        output = '00000000';
    elseif (input > -1*(1/2^20+1)/2^64) && (input < 0)
        output = '80000000';
    else
        pfp_sign = input<0;
        if input<0
            input = -input;
        end
        pfp_exp = floor(log2(input)) + 64;
        pfp_man = round( (input ./ (2.^(pfp_exp - 64)) - 1) * 2^20);
        if(pfp_man >= 2^20)
            pfp_man = 2^20-1;
        end

        pfp_sign = dec2bin(pfp_sign, 1);
        pfp_exp = dec2bin(pfp_exp, 7);
        pfp_man = dec2bin(pfp_man, 20);
        output = [dec2hex(bin2dec([pfp_sign pfp_exp pfp_man]), 7) '0'];
    end
end
function output = pfp2dec(input)

if 4 == length(input) %16bit
    if (strcmp(input, '0000') || strcmp(input, '8000'))
        output = 0;
    else
        input = dec2bin(hex2dec(input), 16);
        pfp_sign = input(1);
        pfp_exp = input(2:6);
        pfp_man = input(7:16);

        pfp_sign = bin2dec(pfp_sign);
        pfp_exp = bin2dec(pfp_exp)-16;
        pfp_man = bin2dec(pfp_man)+1024;

        output = (-1)^pfp_sign * pfp_man * 2^(pfp_exp-10);
    end
else %28bit
    if (strcmp(input, '00000000') || strcmp(input, '80000000'))
        output = 0;
    else
        input = dec2bin(hex2dec(input), 32);
        pfp_sign = input(1);
        pfp_exp = input(2:8);
        pfp_man = input(9:28);

        pfp_sign = bin2dec(pfp_sign);
        pfp_exp = bin2dec(pfp_exp)-64;
        pfp_man = bin2dec(pfp_man)+2^20;

        output = (-1)^pfp_sign * pfp_man * 2^(pfp_exp-20);
    end
end

 

    原文作者:qnczmf
    原文地址: https://blog.csdn.net/u012991043/article/details/97238753
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞