添加主机字节序判断助手函数

master
zcy 2022-01-05 16:41:05 +08:00
parent 9a1f5494b3
commit 799629c5d2
3 changed files with 42 additions and 6 deletions

View File

@ -9,6 +9,11 @@
#include <sys/types.h>
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);

View File

@ -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;
}
}

View File

@ -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 "<<HostByteOrder()<<std::endl;
std::cout<< LimitFloat(1.123,2)<<std::endl;
std::cout<<LimitFloat(1.123,3) << std::endl;
std::cout<<LimitFloat(0.789,1) << std::endl;