From 7751ae2808c07ab35192ace01451078520634c98 Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 10 May 2018 18:24:42 +0800 Subject: [PATCH] shared_func.h: add functions float2buff / buff2float, double2buff / buff2double --- HISTORY | 4 ++++ src/shared_func.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/HISTORY b/HISTORY index b60b68e..2cbab0c 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,8 @@ +Version 1.38 2018-05-10 + * connection_pool.c: set err_no to 0 when success + * shared_func.h: add functions float2buff / buff2float, double2buff / buff2double + Version 1.37 2018-02-24 * ini_file_reader.c function annotations LOCAL_IP_GET support index, such as: #@function LOCAL_IP_GET diff --git a/src/shared_func.h b/src/shared_func.h index 1aee72b..00e42ef 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -192,6 +192,53 @@ void long2buff(int64_t n, char *buff); */ int64_t buff2long(const char *buff); + +/** 32 bits float convert to buffer (big-endian) + * parameters: + * n: 32 bits float value + * buff: the buffer, at least 4 bytes space, no tail \0 + * return: none +*/ +static inline void float2buff(float f, char *buff) +{ + int2buff(*((int *)&f), buff); +} + +/** buffer convert to 32 bits float + * parameters: + * buff: big-endian 8 bytes buffer + * return: 32 bits float value +*/ +static inline float buff2float(const char *buff) +{ + int n; + n = buff2int(buff); + return *((float *)&n); +} + +/** double (64 bits) convert to buffer (big-endian) + * parameters: + * n: 64 bits double value + * buff: the buffer, at least 8 bytes space, no tail \0 + * return: none +*/ +static inline void double2buff(double d, char *buff) +{ + long2buff(*((int64_t *)&d), buff); +} + +/** buffer convert to 64 bits double + * parameters: + * buff: big-endian 8 bytes buffer + * return: 64 bits double value +*/ +static inline double buff2double(const char *buff) +{ + int64_t n; + n = buff2long(buff); + return *((double *)&n); +} + /** trim leading spaces ( \t\r\n) * parameters: * pStr: the string to trim