2

I'm trying to read single precision floating point numbers from a binary (.segy) file on windows, using C++. These numbers follow the IBM floating point architecture, so I need to convert them into IEEE-754 floats after reading.

I have found this C code:

https://www.thecodingforums.com/threads/c-code-for-converting-ibm-370-floating-point-to-ieee-754.438469/

Unfortunately it does not compile on windows.

I also found this code:

https://www.codeproject.com/Articles/12363/Transform-between-IEEE-IBM-or-VAX-floating-point-n

This solution seems a bit too complicated for me, and I'm not sure how to change it to read (IBM format) binary data directly.

Is there a simple C++ code available to solve this problem?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
hutkag
  • 21
  • 2
  • 2
    Does it have to be fast? – Bathsheba Apr 15 '19 at 10:07
  • _"Unfortunately it does not compile on windows."_ Can you be more specific about that please? – πάντα ῥεῖ Apr 15 '19 at 10:13
  • 2
    There isn't in standard C++ and asking to recommend a third-party library is off-topic for SO. Make an attempt at writing it yourself, and if it doesn't work then post the question here (including your attempt). – rustyx Apr 15 '19 at 10:15
  • You're going to have to find a library, do it yourself, or just copy an existing function like the one you linked to. The latter is probably the easiest. Just fix whatever's stopping it from compiling on your system. – Shawn Apr 15 '19 at 10:15
  • 1
    The [WIKI article](https://en.wikipedia.org/wiki/IBM_hexadecimal_floating_point) is quite nice. It even provides test cases for unit testing. So writing own version shouldn't be a problem. – Marek R Apr 15 '19 at 10:21
  • just read the IBM float spec and you should be able to convert it easily. But there are already a lot of duplicates: [IBM Single Precision Floating Point data conversion to intended value](https://stackoverflow.com/q/45227913/995714), [Convert from IBM floating point to IEEE floating point standard and Vice Versa- In C#?](https://stackoverflow.com/q/4550367/995714), [java - IBM-IEEE double-precision floating point byte conversion](https://stackoverflow.com/q/37982757/995714), [IBM to IEEE floating point conv](https://stackoverflow.com/q/6399486/995714) – phuclv Apr 15 '19 at 10:25
  • @Bathsheba It doesn't matter if it works. – hutkag Apr 15 '19 at 12:06
  • @πάνταῥεῖ "fatal error: netinet/in.h: No such file or directory" I tried to find this header file but It doesn't seem to be available on windows. – hutkag Apr 15 '19 at 12:07
  • @Shawn I'm a beginner, but I tried to solve this by myself by decomposing the binary representation of a float and calculate the value from the exponent and the fraction. I did this with an IEEE-754 float as a first step, and was intended to rewrite it for IBM float decomposition. However, seeing the complexity of the codes I linked, I started to doubtd that my approach could work. – hutkag Apr 15 '19 at 12:09
  • @EricPostpischil If i remove then the "ntohl" function is undefined. According to this source https://learn.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-ntohl winsock.h contains this function, however if I include that I get the error message: "undefined reference to `_imp__htonl@4' collect2.exe: error: ld returned 1 exit status" – hutkag Apr 15 '19 at 12:44

0 Answers0