本文介绍了容易的hex / float转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个c ++和一个python程序(只有浮点值)之间做一些输入/输出python有一个很好的功能,将浮点值转换为十六进制数和回来,你可以看到在这个链接:





在C ++中有一个简单的方法来类似?并将python输出转换回C ++ double / float?这样我就不会有两个进程之间交换数据时出现舍入错误的问题...



thx的答案!

从您在问题中提供的连结():

示例:

  #include< cstdio> 

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

运行:

  $ g ++ * .cpp&& (python -c'print 12.34.hex()'| ./a.out)
  42.79 == 0x1.5651ecp + 5 
12.34 == 0x1.8ae148p + 3


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!

解决方案

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

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

这篇关于容易的hex / float转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:18