Matlab float转 IEEE hexadecimal strings转uint8 array

Matlab float转 IEEE hexadecimal strings转uint8 array


假设浮点数为X=1,则其单精度表示为0x3f800000,需要拆为8位的十六进制数才能方便发送。

  1. 使用single函数,将X申明为单精度浮点数
    a=single(X);
  2. 使用num2hex函数转换成IEEE hexadecimal strings
    b=num2hex(a);
  3. 将IEEE hexadecimal strings转换成uint32
    c=uint32(hex2dec(b));
  4. 声明一个uint8 array
    d=uint8(zeros(1,4));
  5. 将uint32转换成uint8 array
    d(1)=bitand(bitshift(c,-24),255);
    d(2)=bitand(bitshift(c,-16),255);
    d(3)=bitand(bitshift(c,-8),255);
    d(4)=bitand(c,255);

函数用法
Syntax
B = single(A)

Description
B = single(A) converts the matrix A to single precision, returning that value in B. A can be any numeric object (such as a double). If A is already single precision, single has no effect. Single-precision quantities require less storage than double-precision quantities, but have less precision and a smaller range.

Syntax
num2hex(X)

Description
If X is a single or double precision array with nelements, then num2hex(X) is an n-by-8 or n-by-16 character array in which each row contains the hexadecimal floating-point representation of a number. The same representation is printed with format hex.

Syntax
d = hex2dec(‘hex_value’)

Description
d = hex2dec(‘hex_value’) converts hex_valueto its floating-point integer representation. The argument hex_value is a hexadecimal integer stored as text. If the value of hex_value is greater than the hexadecimal equivalent of the value returned by flintmax, then hex2dec might not return an exact conversion.

Syntax
C = bitand(A,B)
C = bitand(A,B,assumedtype)
objout = bitand(netobj1,netobj2)
Description
example
C = bitand(A,B) returns the bit-wise AND of A and B.

example
C = bitand(A,B,assumedtype)assumes that A and B are ofassumedtype.

Syntax
intout = bitshift(A,k)
intout = bitshift(A,k,assumedtype)
Description
example
intout = bitshift(A,k) returnsA shifted to the left by k bits, equivalent to multiplying by 2k. Negative values of k correspond to shifting bits right or dividing by 2|k|and rounding to the nearest integer towards negative infinity. Any overflow bits are truncated.

If A is an array of signed integers, then bitshift returns the arithmetic shift results, preserving the signed bit when kis negative, and not preserving the signed bit when k is positive.

If k is positive, MATLAB® shifts the bits to the left and inserts k0-bits on the right.

If k is negative and A is nonnegative, then MATLAB shifts the bits to the right and inserts |k| 0-bits on the left.

If k is negative and A is negative, then MATLAB shifts the bits to the right and inserts|k| 1-bits on the left.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值