double to hex string & hex string to double

后端 未结 9 1448
南方客
南方客 2021-01-02 20:51

What I\'m trying to do is to convert a double to hex string and then back to double.

The following code does conversion double-to-hex string.

char *          


        
9条回答
  •  悲哀的现实
    2021-01-02 21:39

    char *doubleToRawString(double x) {
        // Assumes sizeof(long long) == 8.
    
        char *buffer = new char[32];
        sprintf(buffer, "%llx", *(unsigned long long *)&x);  // Evil!
        return buffer;
    }
    
    double rawStringToDouble(const char *s) {
        // Assumes sizeof(long long) == 8.
    
        double ret;
        sscanf(s, "%llx", (unsigned long long *)&ret);  // Evil!
        return ret;
    }
    

提交回复
热议问题