Re: Splitting a float into bytes:

2006-07-26 Thread John Machin

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 & 0x00FF
> byte2 = int_val & 0xFF00
> byte3 = int_val & 0x00FF
> byte4 = int_val & 0xFF00

Errrmmm ... I think you need the right shift operator e.g.
 byte4 = (int_val & 0xFF00) >> 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???

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting a float into bytes:

2006-07-26 Thread Simon Forman
Michael Yanowitz wrote:
> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf
> Of Simon Forman
> Sent: Wednesday, July 26, 2006 2:56 PM
> To: python-list@python.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 & 0x00FF
> > byte2 = int_val & 0xFF00
> > byte3 = int_val & 0x00FF
> > byte4 = int_val & 0xFF00
> >   But if I try to do that with floats I get:
> > >>> pi & 0xFF
> > Traceback (most recent call last):
> >   File "", 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting a float into bytes:

2006-07-26 Thread spartacus
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" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> 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 & 0x00FF
> byte2 = int_val & 0xFF00
> byte3 = int_val & 0x00FF
> byte4 = int_val & 0xFF00
>  But if I try to do that with floats I get:
 pi & 0xFF
> Traceback (most recent call last):
>  File "", 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
>
> 


-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Splitting a float into bytes:

2006-07-26 Thread Michael Yanowitz

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of Simon Forman
Sent: Wednesday, July 26, 2006 2:56 PM
To: python-list@python.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 & 0x00FF
> byte2 = int_val & 0xFF00
> byte3 = int_val & 0x00FF
> byte4 = int_val & 0xFF00
>   But if I try to do that with floats I get:
> >>> pi & 0xFF
> Traceback (most recent call last):
>   File "", 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 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting a float into bytes:

2006-07-26 Thread Simon Forman
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 & 0x00FF
> byte2 = int_val & 0xFF00
> byte3 = int_val & 0x00FF
> byte4 = int_val & 0xFF00
>   But if I try to do that with floats I get:
> >>> pi & 0xFF
> Traceback (most recent call last):
>   File "", 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

-- 
http://mail.python.org/mailman/listinfo/python-list