如何用matlab将十进制(正整数)转化为十六进制
5个回答
展开全部
二进制到十进制:bin2dec(H), 例如 y=bin2dec('100111')
十进制到二进制:dec2bin(H)
类似,十六进制:hex2dec(H) , dec2hex(H)
任意进制:base2dec(S,B),其中,S是数据,B是进制基数。B为2~36之间的整数,S为整形
如八进制转化为十进制:base2dec('11',8)的结果是9
十进制到二进制:dec2bin(H)
类似,十六进制:hex2dec(H) , dec2hex(H)
任意进制:base2dec(S,B),其中,S是数据,B是进制基数。B为2~36之间的整数,S为整形
如八进制转化为十进制:base2dec('11',8)的结果是9
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
matlab实现十进制数与十六进制数互相转换 原创
2019-07-25 12:03:59
qnczmf
码龄7年
关注
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
2019-07-25 12:03:59
qnczmf
码龄7年
关注
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一位、第二位……数字分别设为a、b、c……,则a*(8^0)+b*(8^1)+c*(8^3)=n,so,你可以把要求的十进制数按照8,64,512……分解,把a/b/c试出来就行了,a/b/c必须是0~7之间的数。十六进制同理,按16^0,16^1,16^2……分解即可。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询