easy hex/float conversion

2019-07-19 04:37发布

I am doing some input/output between a c++ and a python program (only floating point values) python has a nice feature of converting floating point values to hex-numbers and back as you can see in this link:

http://docs.python.org/library/stdtypes.html#additional-methods-on-float

Is there an easy way in C++ to to something similar? and convert the python output back to C++ double/float? This way I would not have the problem of rounding errors when exchanging data between the two processes...

thx for the answers!

7条回答
混吃等死
2楼-- · 2019-07-19 04:39

C++11 offers std::hexfloat: IO manipulators including hexfloat

If you can use C++11 then hexfloat is pretty easy to use.

查看更多
smile是对你的礼貌
3楼-- · 2019-07-19 04:41

From the link you provided in your question (Additional Methods on Float):

This syntax is similar to the syntax specified in section 6.4.4.2 of the C99 standard, and also to the syntax used in Java 1.5 onwards. In particular, the output of float.hex() is usable as a hexadecimal floating-point literal in C or Java code, and hexadecimal strings produced by C’s %a format character or Java’s Double.toHexString are accepted by float.fromhex().

Example:

#include <cstdio>

int main() {
  float f = 42.79;
  printf("%.2f == %a\n", f, f);  
  fscanf(stdin, "%a", &f);
  printf("%.2f == %a\n", f, f);  
}

Run it:

$ g++ *.cpp && (python -c'print 12.34.hex()' | ./a.out )

Output:

42.79 == 0x1.5651ecp+5
12.34 == 0x1.8ae148p+3
查看更多
forever°为你锁心
4楼-- · 2019-07-19 04:43

You're sorely mistaken if you believe that this will solve all your problems. The hex notation is exactly equivalent to the original float. It just uses a different representation.

查看更多
手持菜刀,她持情操
5楼-- · 2019-07-19 04:53

The docs there say that %a does this in C.

查看更多
一夜七次
6楼-- · 2019-07-19 04:56

C++ does not have any library routines for converting hex to floating point. One reason is that the internal representation of floating point is not standardized (although many compilers do use an IEEE standard).

I recommend storing in ASCII format, which is portable across platforms. You could write your own library (or find another) which will convert between an IEEE format (in hex) to the internal representation.

查看更多
Anthone
7楼-- · 2019-07-19 04:59

If you're on a platform whose system C library supports C99, you can just use printf( ) and scanf( ) with the %a format specifier. For reading in such values, you can also use the C99 strtod( ), strtof( ), and strtold( ) functions:

float value = strtof(string, NULL, 0);

You can optionally replace NULL with a char ** to get back a pointer to the end of the sequence of converted characters, and 0 with a known base for the string representation (if you set the base to be zero, it will infer the base from the format; it will parse the python hex floats just fine, and also handle normal decimal-formatted floating point)

If your compiler/library vendor has chosen not to care about C99 (MSVC, basically), you're probably out of luck until these function are incorporated into the C++ standard (I believe that they're in the draft C++0x, but I'm not completely sure).

查看更多
登录 后发表回答