Test program for SPS30
Embed:
(wiki syntax)
Show/hide line numbers
conversions.cpp
00001 /** 00002 * @defgroup conversions Data Structure Conversions 00003 * @{ 00004 */ 00005 00006 #include <sstream> 00007 #include <iomanip> 00008 #include <vector> 00009 #include "conversions.h" 00010 00011 /** 00012 * @brief Converts a C++ string to C-string; Use free() to release mem after copying the content. 00013 * @author Lau Lee Hong 00014 * @param str C++ string 00015 * @return Pointer to a null-terminated array of character 00016 */ 00017 char* StringToChar(const std::string& str) 00018 { 00019 char* buffer = (char*)std::malloc(std::strlen(str.c_str()) + 1); 00020 if(buffer != NULL) 00021 { 00022 std::strcpy(buffer, str.c_str()); 00023 } 00024 00025 return buffer; 00026 } 00027 00028 /** 00029 * @brief Converts an int-type to string-type hex. 00030 * @author Lau Lee Hong 00031 * @param i Integer to be converted to hex string 00032 * @return Hexadecimal of string format 00033 */ 00034 std::string IntToHex(uint32_t i) 00035 { 00036 std::stringstream stream; 00037 stream << std::hex << i; 00038 return stream.str(); 00039 } 00040 00041 /** 00042 * @brief Converts an int-type to string. 00043 * @author Lau Lee Hong 00044 * @param i (int)integer to be converted (std::string)integer 00045 * @return Integer of string format 00046 */ 00047 std::string IntToString(int v) 00048 { 00049 std::ostringstream oss; 00050 oss << v; 00051 return oss.str(); 00052 } 00053 00054 /** 00055 * @brief Converts an double-type to a C-string with variable number of decimal places. 00056 * @author Lau Lee Hong 00057 * @param str buffer to store result 00058 * @param v value 00059 * @param decimal_digits number of decimal places 00060 * @return Null-terminated charcter array of double value with user-defined number of decimal places 00061 */ 00062 char* DoubleToChar(char* str, double v, int decimal_digits) 00063 { 00064 int i = 1; 00065 int int_part, fract_part; 00066 int len; 00067 char *ptr; 00068 00069 /* Prepare decimal digits multiplicator */ 00070 for (; decimal_digits != 0; i *= 10, decimal_digits--); 00071 00072 /* Calculate integer & fractional parts */ 00073 int_part = (int)v; 00074 fract_part = (int)((v - (double)(int)v) * i); 00075 00076 /* Fill in integer part */ 00077 std::sprintf(str, "%i.", int_part); 00078 00079 /* Prepare fill in of fractional part */ 00080 len = std::strlen(str); 00081 ptr = &str[len]; 00082 00083 /* Fill in leading fractional zeros */ 00084 for (i /= 10; i > 1; i /= 10, ptr++) 00085 { 00086 if (fract_part >= i) 00087 { 00088 break; 00089 } 00090 *ptr = '0'; 00091 } 00092 00093 /* Fill in (rest of) fractional part */ 00094 std::sprintf(ptr, "%i", fract_part); 00095 00096 return str; 00097 } 00098 00099 /** 00100 * @brief Converts a C++ string to an integer 00101 * @author Lee Tze Han 00102 * @param str C++ string 00103 * @return Integer from string representation 00104 */ 00105 int StringToInt(const std::string& str) 00106 { 00107 std::istringstream iss(str); 00108 int v; 00109 iss >> v; 00110 return v; 00111 } 00112 00113 /** 00114 * @brief Converts a time_t object (from NTPClient) to a C++ string 00115 * @author Lee Tze Han 00116 * @param time time_t representing current time 00117 * @return String representation of the underlying typedef'd uint32_t 00118 */ 00119 //std::string TimeToString(const std::time_t time) 00120 //{ 00121 // std::ostringstream oss; 00122 // oss << time; 00123 // return oss.str(); 00124 //} 00125 // 00126 ///** 00127 // * @brief Converts a C++ string to time_t object 00128 // * @author Lee Tze Han 00129 // * @param str C++ string 00130 // * @return time_t from string representation 00131 // */ 00132 //std::time_t StringToTime(const std::string& str) 00133 //{ 00134 // std::istringstream iss(str); 00135 // std::time_t time; 00136 // iss >> time; 00137 // return time; 00138 //} 00139 00140 /** @}*/
Generated on Tue Jul 26 2022 07:43:19 by
1.7.2