matlab str2bit,Matlab中数字的二进制表示

在Matlab中,可以使用Java JDK函数.

将Matlab中的float(单精度32位数)转换为二进制字符串表示的简短答案可能是:

flt=3.14

import java.lang.Integer java.lang.Float;

Integer.toBinaryString(Float.floatToIntBits(flt))

答案很长:在Matlab中将float(单精度32位数)转换为二进制字符串表示

function out=float2binstring(flt)

% converts a float number to binary in matlab according to IEEE754

%

% Usage:

% float2binstring(-3.14)

%

% http://www.h-schmidt.net/FloatApplet/IEEE754.html

%

% Limitations:

% Rounding errors: Not every decimal number can be expressed exactly as a floating

% point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit.

%

% Andrej Mosat, nospam@mosi.sk

% 03/2012

% v0.0

% License: GNU GPL

%

% See also: BINSTRING2FLOAT

% this is a trick to use java JDK should be installed, tested on Matlab R2010

import java.lang.Integer java.lang.Float;

if ( ~isnumeric(flt) )

error('input must be a number');

end

out=Integer.toBinaryString(Float.floatToIntBits(flt));

end

并且二进制字符串转换为float,只需要一点开销:

function out=binstring2float(binstr)

% converts a binary string to float number according to IEEE754

%

% Usage:

% binstring2float('11000000010010001111010111000011')

% -3.14

%

%

% http://www.h-schmidt.net/FloatApplet/IEEE754.html

%

% Limitations:

% Rounding errors: Not every decimal number can be expressed exactly as a floating

% point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit.

%

% Andrej Mosat, nospam@mosi.sk

% 03/2012

% v0.0

% License: GNU GPL

%

% See also: FLOAT2BINSTRING

import java.lang.Long java.lang.Float;

if isequal(class(binstr), 'java.lang.String')

binstr=char(binstr);

end

if ( ~isstr(binstr) )

error('input must be a binary string');

end

% Error handling for binary strings should be added here

% the sign is negative

if binstr(2)=='1'

binstr(2)='';

isnegative=1;

else

isnegative=0;

end

out=Float.intBitsToFloat( Long.parseLong( binstr , 2) );

if isnegative

out=-out;

end

end

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值