diff --git a/general/inc/utils.h b/general/inc/utils.h index 58e2ba8..bb127ac 100644 --- a/general/inc/utils.h +++ b/general/inc/utils.h @@ -9,6 +9,11 @@ #include using namespace std; +// 字节序选择 +typedef enum{ + BIG_ENDIAN, // 大端 + LITTLE_ENDIAN // 小端 +}BYTE_ORDER; typedef enum { ENV_WINDOWS = 1, @@ -27,13 +32,17 @@ inline ENV_SYS CurrentEnvSys(); inline ENV_COMPILER CurrentEnvCompiler(); #define RELEASE_MEMORY(x) \ - if(nullptr == x) \ - { \ - delete(x); \ - x = nullptr;\ - } + do { \ + if(nullptr == x) \ + { \ + delete(x); \ + x = nullptr;\ + } \ + }while(0) + string itos(int x); +BYTE_ORDER HostByteOrder(); // 限制float精确度 float LimitFloat(float in,int size); diff --git a/general/src/utils.cpp b/general/src/utils.cpp index f2a2d93..4149236 100644 --- a/general/src/utils.cpp +++ b/general/src/utils.cpp @@ -64,7 +64,7 @@ inline ENV_COMPILER CurrentEnvCompiler() // 限制float有效位 -float LimitFloat(float in,int size){ +float LimitFloat(float in,int size) { uint64_t tmp = 1; for(int i = 0;i < size;i++){ tmp = tmp*10; @@ -76,3 +76,14 @@ float LimitFloat(float in,int size){ return float(integer)/tmp; } + + +BYTE_ORDER HostByteOrder(){ + short x = 0x1234; + uint8_t y = *((uint8_t*)(&x)); + if(y == 0x34){ + return BYTE_ORDER::LITTLE_ENDIAN; + }else{ + return BYTE_ORDER::BIG_ENDIAN; + } +} diff --git a/test/src/cpp11/log_test.cpp b/test/src/cpp11/log_test.cpp index 6e07587..3b54b07 100644 --- a/test/src/cpp11/log_test.cpp +++ b/test/src/cpp11/log_test.cpp @@ -31,7 +31,23 @@ float LimitFloat(float in,int size){ return float(integer)/tmp; } +typedef enum{ + BIG_ENDIAN = 1, // 大端 + LITTLE_ENDIAN // 小端 +}BYTE_ORDER; + +BYTE_ORDER HostByteOrder(){ + short x = 0x1234; + uint8_t y = *((uint8_t*)(&x)); + if(y == 0x34){ + return BYTE_ORDER::LITTLE_ENDIAN; + }else{ + return BYTE_ORDER::BIG_ENDIAN; + } +} + int main(){ + std::cout<<"byteorder "<