Matlab有符号十进制小数转换为有符号十六进制数

您所在的位置:网站首页 十六进制数的表示符号 Matlab有符号十进制小数转换为有符号十六进制数

Matlab有符号十进制小数转换为有符号十六进制数

2024-07-03 02:43| 来源: 网络整理| 查看: 265

实现内容:

1) 通过Matlab将生成的随机小数扩大为20位有符号位 2) 将拓展后的数据变为补码格式: 负数变为补码,正数保持不变 3)将补码格式的十进制小数转换为有符号十六进制数 4)将生成的十六进制数写入.coe文件中

代码实现 % generate the random number for i = 1: 10 M(i,1)= (-1)^i *rand(1); end N = length(M); Radix = 16; Coeff_width = 20; % convert the fraction number to integer % 20 bits with 1 signed bit M_mul = round(M.*2^(Coeff_width-1)); % convert the coefficient to complement format for i = 1: N if(M_mul(i) < 0) comple_M(i) = 2^Coeff_width + M_mul(i); else comple_M(i) = M_mul(i); end end %% write the real part .coe file fid = fopen('M.coe','w');%File storage path fprintf(fid,'Radix = %d; \n',Radix); fprintf(fid,'Coefficient_Width = %d; \n',Coeff_width); fprintf(fid,'CoefData = '); % convert the complem


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3