Matlab float转 IEEE hexadecimal strings转uint8 array
假设浮点数为X=1,则其单精度表示为0x3f800000,需要拆为8位的十六进制数才能方便发送。
- 使用single函数,将X申明为单精度浮点数
a=single(X); - 使用num2hex函数转换成IEEE hexadecimal strings
b=num2hex(a); - 将IEEE hexadecimal strings转换成uint32
c=uint32(hex2dec(b)); - 声明一个uint8 array
d=uint8(zeros(1,4)); - 将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.