I read 4 bytes from a binary file.
These bytes represent a floating point number (mantisse exponent form)
How can I get a float from these bytes ? 4 8351
Jean-Baptiste PERIN <jb******@yahoo .fr> wrote: I read 4 bytes from a binary file.
These bytes represent a floating point number (mantisse exponent form)
How can I get a float from these bytes ?
See the docs for module struct, specifically the struct.unpack function,
if the float is in the binary format your machine expects (or possibly
that but with the wrong endianity). If the float's binary format is
different from what your machine uses, you're in for a lot of painful
bit-twiddling.
Alex
Alex Martelli a écrit : Jean-Baptiste PERIN <jb******@yahoo .fr> wrote:
I read 4 bytes from a binary file.
These bytes represent a floating point number (mantisse exponent form)
How can I get a float from these bytes ?
See the docs for module struct, specifically the struct.unpack function, if the float is in the binary format your machine expects (or possibly that but with the wrong endianity). If the float's binary format is different from what your machine uses, you're in for a lot of painful bit-twiddling.
Alex
I'll have to handle Intel-PC, DEC-VAX and MIPS-SUN/SGI numbers
So I can't escape the painful bit-twiddling
Anyway, struct.unpack will undoubtedly be an incredibly valuable help
thank you very very very much ..
JiBé
Jean-Baptiste PERIN <jb******@yahoo .fr> wrote: I'll have to handle Intel-PC, DEC-VAX and MIPS-SUN/SGI numbers So I can't escape the painful bit-twiddling
I don't recall for sure (even though I did my thesis on a Vax, 25 years
ago!) but I think you _might_ be lucky -- VAX used the binary format
that became the IEEE standard, if I recall correctly.
Intel, MIPS, SUN and SGI surely did follow IEEE standards, endianity
apart, and you can correct for endianity with struct.unpack.
The problem would be there if you had, say, floats in old IBM 360/370
formats, or Cray's original formats, or the like...
Anyway, struct.unpack will undoubtedly be an incredibly valuable help
thank you very very very much ..
You're welcome!
Alex
Alex Martelli wrote: I don't recall for sure (even though I did my thesis on a Vax, 25 years ago!) but I think you _might_ be lucky -- VAX used the binary format that became the IEEE standard, if I recall correctly.
iirc, you have to swap bytes around. the code on this page might
be helpful: http://www.octave.org/octave-lists/a.../msg00033.html
The problem would be there if you had, say, floats in old IBM 360/370 formats, or Cray's original formats, or the like...
here's a IBM 360 converter (at least that's what I think it is; the code is taken
from a PIL format converter for a format that uses "IBM floats"):
def ibm_f32s(c):
a = ord(c[0]) & 127
b = ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16)
v = pow(2.0, -24) * b * pow(16.0, a-64)
if ord(c[0]) > 127:
return -v
return v
many floating point formats are trivial variations on this theme.
</F> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sam Smith |
last post by:
Hi,
I wan't a function to take a const char*, a start bit position and number of
bits and convert that bit-stream into a primitive of desired type.
I.e. something like:
char convert(const unsigned char* buffer, size_t start_pos, size_t length)
{
char value = 0;
|
by: Cally |
last post by:
Hello,
I would like to convert a field from ntext field found in one database
table to float field found in another database table. The reason why I
want to do this is a long one.
I have tried the following and playing around with the following:
declare @valuePointer varbinary(16)
|
by: GRoll35 |
last post by:
I get 4 of those errors. in the same spot. I'll show my parent class,
child class, and my driver.
All that is suppose to happen is the user enters data and it uses
parent/child class to display it.
here is the 4 errors.
c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from
'char ' to 'char '
|
by: Michael Yanowitz |
last post by:
Hello:
For some reason I can't figure out how to split
a 4-byte (for instance) float number (such as 3.14159265359)
into its 4-bytes so I can send it via a socket to another
computer.
For integers, it is easy, I can get the 4 bytes by anding like:
byte1 = int_val & 0x000000FF
byte2 = int_val & 0x0000FF00
byte3 = int_val & 0x00FF0000
|
by: SpreadTooThin |
last post by:
I have some code...
import array
a = array.array('d')
f = open('file.raw')
a.fromfile(f, 10)
now I need to convert them into floats (32 bit...) what do i do?
| |
by: Mario M. Mueller |
last post by:
Hi,
I have a binary file containing 3 byte float values (big endian). How can I
read them into python? The struct module does not work, since it expects 4
byte floats.
Any hints?
Mario
|
by: Cross |
last post by:
I am building a client-server application. When the clients gets the data
from the server, the data is received as a byte array. The server works with
data in 2-dimentional arrays (recordsets received from SQL Server, could be
integers, float, string dates...) stored as the data type objects. So I need
to convert the arrays to bytes on the server, and then back to the orginal
2-dimensional array on the client side. What is the...
|
by: Mason |
last post by:
I have tried and tried...
I'd like to read in a binary file, convert it's 4 byte values into
floats, and then save as a .txt file.
This works from the command line (import struct);
In : f = open("test2.pc0", "rb")
In : tagData = f.read(4)
In : tagData
|
by: Peter |
last post by:
Does anyone know how to convert the following VB6 code to C# code?
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As
Any, Source As Any, ByVal bytes As Long)
Dim sngArray(0) As Single
strString = Chr$(107) & Chr$(62) & Chr(139) & Chr$(65)
CopyMemory sngArray(0), ByVal strString, Len(strString)
After running the code sngArray(0) = 17.40548
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |