double to hex string & hex string to double

后端 未结 9 1426
南方客
南方客 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条回答
  •  萌比男神i
    2021-01-02 21:44

    I am using the below function, to convert double to hexadecimal.

    char * double2HexString(double a) 
    { 
       char *buf = new char[17]; // double is 8-byte long, so we have 2*8 + terminating \0 
       char *d2c; 
       d2c = (char *) &a; 
       char *n = buf; 
       int i; 
       for(i = 0; i < 8; i++) 
       { 
          sprintf(n, "%02X", *d2c++); 
          n += 2; 
       }  
       *(n) = '\0'; 
    }  
    

    passing 1.0 & 16.0 to this function, the return values observed are 000000000000FF37 & 0000000000003040 respectively. I think we should get 1 & 10.

提交回复
热议问题