473,770 Members | 5,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to convert 4 bytes into a float ?


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 ?

Jul 18 '05 #1
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
Jul 18 '05 #2
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é
Jul 18 '05 #3
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
Jul 18 '05 #4
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>

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2526
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;
5
18536
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)
12
10090
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 '
4
5375
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
7
3892
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?
7
7252
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
2
1559
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...
4
22314
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
4
4660
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
0
9592
marktang
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...
0
9425
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,...
1
10005
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,...
0
8887
agi2029
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...
0
6679
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();...
0
5313
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...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.