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
byte4 = int_val & 0xFF000000
But if I try to do that with floats I get:
>>pi & 0xFF
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for &: 'float' and 'int'
Is there some easy way to get what the bytes of the float are?
Thanks in advance:
Michael Yanowitz 4 5363
Michael Yanowitz wrote:
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
byte4 = int_val & 0xFF000000
But if I try to do that with floats I get:
>pi & 0xFF
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for &: 'float' and 'int'
Is there some easy way to get what the bytes of the float are?
Thanks in advance:
Michael Yanowitz
The struct module. (It also works for ints. ;-) ) http://docs.python.org/lib/module-struct.html
HTH,
~Simon
Yes i think you can. If you use the struct module.
>import struct import math y = struct.pack('!f ', math.pi) print repr(y)
'@I\x0f\xdb'
"Michael Yanowitz" <m.********@kea rfott.comwrote in message
news:ma******** *************** *************** *@python.org...
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
byte4 = int_val & 0xFF000000
But if I try to do that with floats I get:
>>>pi & 0xFF
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for &: 'float' and 'int'
Is there some easy way to get what the bytes of the float are?
Thanks in advance:
Michael Yanowitz
Michael Yanowitz wrote:
-----Original Message-----
From: py************* *************** *************@p ython.org
[mailto:py****** *************** *************** *****@python.or g]On Behalf
Of Simon Forman
Sent: Wednesday, July 26, 2006 2:56 PM
To: py*********@pyt hon.org
Subject: Re: Splitting a float into bytes:
Michael Yanowitz wrote:
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
byte4 = int_val & 0xFF000000
But if I try to do that with floats I get:
>>pi & 0xFF
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for &: 'float' and 'int'
Is there some easy way to get what the bytes of the float are?
Thanks in advance:
Michael Yanowitz
The struct module. (It also works for ints. ;-) )
http://docs.python.org/lib/module-struct.html
HTH,
~Simon
Thanks, but maybe I am missing something.
If I use pack, doesn't it have to be unpacked at the other end
to make sense? The data will be picked up on some other computer
by some other application probably written in C or C++. Would
it have to be rewritten to unpack the data?
Thanks in advance:
Michael Yanowitz
It says in the docs "This module performs conversions between Python
values and C structs represented as Python strings." This means, to
me, that the packed data will be in the format that C (and C++ I would
assume) uses.
You might have to mind the endian-ness if you're transferring data
between different architectures, but struct includes format specifiers
for this.
Have fun,
~Simon
Michael Yanowitz wrote:
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
byte4 = int_val & 0xFF000000
Errrmmm ... I think you need the right shift operator e.g.
byte4 = (int_val & 0xFF000000) >24
but assert 0 <= byte4 <= 255 may fail under some circumstances
so (better):
byte4 = (int_val >24) & 0xFF
# absolutely guaranteed to pass that assertion
BUT why muck around with all that when you can use struct.pack??? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: franzkowiak |
last post by:
Hello,
I've read some bytes from a file and just now I can't interpret 4 bytes
in this dates like a real value.
An extract from my program
def l32(c):
return ord(c) + (ord(c)<<8) + (ord(c)<<16) + (ord(c)<<24)
|
by: Rakesh |
last post by:
Hi,
I was 'googling' to look out for some ways of optimizing the code
and came across this term - 'hot / cold splitting'.
In short, the discussion is about splitting heavily accessed ( hot )
portions of data structure from rarely accessed cold portions.
I haven't used this one myself anytime before, but am interested in
learning more about this.
Can you please share your experience here, so that I can understand
better and this could...
|
by: Mike |
last post by:
I am trying to calculate a 32bit float value from 4 int values.
I sucessfully calcluated a 32bit long value:
LONG l32BitData = pData;
l32BitData <<= 8;
l32BitData |= (byte)pData;
l32BitData <<= 8;
l32BitData |= (byte)pData;
|
by: Michael |
last post by:
Hallo NG,
a few jears ago i made myself in c++ a funktion to convert 4 Byte to one
float variable:
//###########################################################
// Make from 4 Bytes one float variable wich is 4 bytes long
//###########################################################
float CAtgGalvanoMachine::Convert4BytesToFloat(BYTE byte1, BYTE byte2, BYTE
byte3, BYTE byte4)
|
by: Codex Twin |
last post by:
Hi
I have a common model for a Data Access Layer scenario. I have an abstract
base class, called DalBase which contains a list of abstract methods. Lets
call them:
public abstract void Shine();
public abstract void Flow();
public abstract void Float();
I then have an inherited class, called DalMain which contains the concrete
| |
by: Gregory.A.Book |
last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have
a library that reads image data and returns the data as an array of
unsigned chars. The image data is stored as 4-byte floats. How can I
convert the sets of 4 bytes to floats?
Thanks,
Greg Book
|
by: dzar |
last post by:
I have an application that sends messages to other applications through
PostMessage(HWND_BROADCAST, MY_MESSAGE_ID, wparam_float, lparam_float);
in C (and this works... I can typecast and built byte arrays with
whatever I want in C and extract them at the other end just fine).
I am now needing to interface with C# and find I cannot do this in an
easy way (at least for me). When I try something like:
Single sValue= (Single) m.LParam;
|
by: Yasin Cepeci |
last post by:
I ve get float data from serial port. I ve taken it in the form of hex by
modbus protocol. I know it is float but I couldnt convert it
there is a few sample data below;
B3 33 43 34 = 180.699997
33 33 43 33 = 179.199997
B3 33 43 34= 180.699997
CC CD 43 33=179.800003
But how can I found it. I couldnt resolve it.
|
by: boxfish |
last post by:
Hi,
I'm trying to write code in C++ that loads .3ds files. .3ds files use floats in binary for vertex coordinates, but I don't know how to read them. I googled stuff for a bit, until it seemed like reinterpret_cast was the way to do it, but this code
float readFloat(ifstream& file) {
char bytes;
for (int i = 0; i < sizeof(float); i++)
bytes = file.get();
return reinterpret_cast<float>(bytes);
}
gives me this error:...
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: 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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |