I am trying to convert some files from binary, to plain text.
I have no documentation on the file format of the binary files, but I do have examples to use of both the binary file, and it's text equavalent.
OK, the first filetype went fine - by comparing the two files, I was able to work out the file format, what parts converted to asci equavalents, what parts were integer numbers, and which were floats.
Now, I'm looking at the second file type, but in addition to asci text, integers, and floats, I find some numbers like these:
-2.62766e-012
-5.88894e-005
What the heck are those? More importantly, how would I convert them from a hex number in the file to text output like that?
I load the whole binary file into memory, then I read the floats by PeekF into a float variable, the integers by PeekL into a long variable.
How would I read this type of number? And print it in that format as above?
Math Experts Question
-
- Enthusiast
- Posts: 252
- Joined: Fri Feb 20, 2004 5:43 pm
Yeah, waffle's right. Sometimes an e is used like that to mean *10^. So -2.62766e-012 means -2.62766*(10^-12). I think the reason for this is that a float cant store enough decimal places to represent numbers like these. But if you were wanting to display them as decimals you could use strings and just shift the decimal place along.
They are stored like this so that number accuracy is independent of number size.-2.62766e-012
-5.88894e-005
What the heck are those? More importantly, how would I convert them from a hex number in the file to text output like that?
You need to know what format these floats are stored in, for example IEEE double and IEEE single are 2 such formats.
These formats specify which bits in the binary (or hex) number are used for storing the sign, the mantissa (the 5.88894 bit) and the exponent (the e-012 bit)
If the format is the same 4 byte float format used by Purebasic then you are lucky and can use the PB command ReadFloat() to read these bytes into a float variable. If the format is different then you must separate out the different parts of the number in your code and assemble the float number accordingly.
If the format is IEEE 8 byte float (ie double) then you will have to wait a short time until this format in incorporated into pureBasic.