how to convert double between host and network byte order?

后端 未结 2 1606
深忆病人
深忆病人 2020-12-06 15:41

Could somebody tell me how to convert double precision into network byte ordering. I tried

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostsh         


        
2条回答
  •  遥遥无期
    2020-12-06 16:09

    This might be hacky (the char* hack), but it works for me:

    double Buffer::get8AsDouble(){
    
        double little_endian = *(double*)this->cursor;
    
        double big_endian;
    
        int x = 0;
        char *little_pointer = (char*)&little_endian;
        char *big_pointer = (char*)&big_endian;
    
        while( x < 8 ){
            big_pointer[x] = little_pointer[7 - x];
            ++x;
        }
    
        return big_endian;
    
    }
    

    For brevity, I've not include the range guards. Though, you should include range guards when working at this level.

提交回复
热议问题