tags: Experience sharing
This requirement is often used in serial port communication, and the serial port can only communicate with a character type (char). If you want to start floating point (FLOAT), the method of using floating point turning strings is more troublesome.
Use library functions:
The C / C ++ language provides several standard library functions that can be converted to any type (integer, long integer, floating point, etc.).
ATOF (): Converts strings to double precision floating point values.
ATOI (): Converts strings to integer values.
But they don't apply to cross-platform application scenarios.
Then you can use another function
sprintf function format: int sprintf (char * buffer, const char * format [, argument, ...];
In addition to the first two parameters, the optional parameters can be arbitrary. Buffer is a character number of characters; Format is a format string.
With memory storage format conversion, the method handles data to be greatly simplified, and can turn 4 bytes of characters to two bytes, which greatly increase the transmission efficiency, of course, if you want to pass double precision floating point Double Double Double Or if the accuracy is to reach the four digits of the decimal point, then use the memory address replication, the library function is
void *memcpy(void *destin, void *source, unsigned n)
Define a common body
union Fp32
{
uint32_t u;
float f;
};
Floating point Turn UINT16 function
uint16_t float_cov_uint16(float value)
{
const Fp32 f32infty = { 255U << 23 };
const Fp32 f16infty = { 31U << 23 };
const Fp32 magic = { 15U << 23 };
const uint32_t sign_mask = 0x80000000U;
const uint32_t round_mask = ~0xFFFU;
Fp32 in;
uint16_t out;
in.f = value;
uint32_t sign = in.u & sign_mask;
in.u ^= sign;
if (in.u >= f32infty.u) /* Inf or NaN (all exponent bits set) */
{
/* NaN->sNaN and Inf->Inf */
out = (in.u > f32infty.u) ? 0x7FFFU : 0x7C00U;
}
else /* (De)normalized number or zero */
{
in.u &= round_mask;
in.f *= magic.f;
in.u -= round_mask;
if (in.u > f16infty.u)
{
in.u = f16infty.u; /* Clamp to signed infinity if overflowed */
}
out = uint16_t(in.u >> 13); /* Take the bits! */
}
out = uint16_t(out | (sign >> 16));
return out;
}
UINT16 turn Float function
float uint6_cov_float(uint16_t value)
{
const Fp32 magic = { (254U - 15U) << 23 };
const Fp32 was_infnan = { (127U + 16U) << 23 };
Fp32 out;
out.u = (value & 0x7FFFU) << 13; /* exponent/mantissa bits */
out.f *= magic.f; /* exponent adjust */
if (out.f >= was_infnan.f) /* make sure Inf/NaN survive */
{
out.u |= 255U << 23;
}
out.u |= (value & 0x8000U) << 16; /* sign bit */
return out.f;
}
C language float type Floating point variables are composed of limited storage units Therefore, only a limited number of significant digits can be provided, and the numbers outside the significant dig...
Problem Description Suppose there is such a demand now, it willdoubleType data transitionfloatBecause C ++ does not perform hidden type conversion, the general approach is to usefloatForced conversion...
Contents Index Traditional conversion modes, and user-defined conversion const_cast reinterpret_cast static_cast dynamic_cast Why is there talk about the four conversion operator It seems I should hav...
With stringstream E.g: It is noted that, if placed inside the loop stringstream need to change the value every time, before the cycle to use clear () method of each entry. Otherwise, if the definition...
Recently, I encountered the need to convert the data type to and according to the data in the file storage before saving. I learned these two methods: Can be used below You can also use union Transf...
float can also be in the form of a structure...
String Type and Char * Type Conversion Method Data () method using String class Use the c_str () method of the String class Use the string class COPY method...
problem: code show as below: The compiler will convert i to a float type and then compare the size of the two floats, but can float represent all ints? Why not convert int and float to double type for...