c语言uint8转float32,float uint32 uint16 转为uint8

appendUint32ToVec(std::vector & vec, unsigned int val, bool bigEndian )

{

if (bigEndian)

{

// unsigned char* p = (unsigned char*) &val;

// p += 3;

// vec.push_back(*p--);

// vec.push_back(*p--);

// vec.push_back(*p--);

// vec.push_back(*p);

uint8_t p = (val>>24)&0xFF;

vec.push_back(p);

p = (val>>16)&0xFF;

vec.push_back(p);

p = (val>>8)&0xFF;

vec.push_back(p);

p = (val&0xFF);

vec.push_back(p);

}

else

{

// unsigned char* p = (unsigned char*) &val;

// vec.push_back(*p++);

// vec.push_back(*p++);

// vec.push_back(*p++);

// vec.push_back(*p++);

uint8_t p = (val&0xFF);

vec.push_back(p);

p = (val>>8)&0xFF;

vec.push_back(p);

p = (val>>16)&0xFF;

vec.push_back(p);

p = (val>>24)&0xFF;

vec.push_back(p);

}

}

appendUint16ToVec(std::vector & vec, unsigned short val, bool bigEndian )

{

if (bigEndian)

{

// unsigned char* p = (unsigned char*) &val;

// p += 1;

// vec.push_back(*p--);

// vec.push_back(*p);

vec.push_back((val>>8)&0xFF);

vec.push_back(val&0xFF);

}

else

{

// unsigned char* p = (unsigned char*) &val;

// vec.push_back(*p++);

// vec.push_back(*p++);

vec.push_back(val&0xFF);

vec.push_back((val>>8)&0xFF);

}

}

appendFloadToVec(std::vector & vec, float val, bool bigEndian)

{

if (bigEndian)

{

unsigned char* p = (unsigned char*) &val;

p+= 3;

vec.push_back(*p--);

vec.push_back(*p--);

vec.push_back(*p--);

vec.push_back(*p);

}

else

{

unsigned char* p = (unsigned char*) &val;

vec.push_back(*p++);

vec.push_back(*p++);

vec.push_back(*p++);

vec.push_back(*p++);

}

}

float 也可以用结构体形式

你可能感兴趣的:(c语言uint8转float32,float uint32 uint16 转为uint8)