How can I assign a float variable to an unsigned int variable, bit image, not cast

后端 未结 3 1016
无人及你
无人及你 2021-01-13 16:28

I know this is a bizarre thing to do, and it\'s not portable. But I have an allocated array of unsigned ints, and I occasionaly want to \"store\" a float in it. I don\'t w

3条回答
  •  离开以前
    2021-01-13 16:52

    This can be achieved through a simple copy:

    uint32_t dst;
    float    src = get_float();
    
    char * const p = reinterpret_cast(&dst);
    
    std::copy(p, p + sizeof(float), reinterpret_cast(&src));
    
    // now read dst
    

    Copying backwards works similarly.

提交回复
热议问题