FAQ
Hi,

I'm currently trying to receive data from a socket with
data = mysock.recv(LENGTH)
the fisrt byte of data is the length of data to receive.
If I print the length I get : print data[0] -> "\x04"
How can I "cast" it in order to handle it as an int whose value is 4 ?

thanks

Search Discussions

  • Ian Bicking at Feb 18, 2003 at 9:23 am

    On Tue, 2003-02-18 at 03:15, Franck Bui-Huu wrote:
    I'm currently trying to receive data from a socket with
    data = mysock.recv(LENGTH)
    the fisrt byte of data is the length of data to receive.
    If I print the length I get : print data[0] -> "\x04"
    How can I "cast" it in order to handle it as an int whose value is 4 ?
    ord("\x04") == 4

    --
    Ian Bicking ianb at colorstudy.com http://colorstudy.com
    4869 N. Talman Ave., Chicago, IL 60625 / 773-275-7241
    "There is no flag large enough to cover the shame of
    killing innocent people" -- Howard Zinn
  • Andrew Bennetts at Feb 18, 2003 at 10:14 am

    On Tue, Feb 18, 2003 at 03:23:52AM -0600, Ian Bicking wrote:
    On Tue, 2003-02-18 at 03:15, Franck Bui-Huu wrote:
    I'm currently trying to receive data from a socket with
    data = mysock.recv(LENGTH)
    the fisrt byte of data is the length of data to receive.
    If I print the length I get : print data[0] -> "\x04"
    How can I "cast" it in order to handle it as an int whose value is 4 ?
    ord("\x04") == 4
    And for larger values, e.g. 2-byte or 4-byte ints, you'll need the struct
    module.

    -Andrew.
  • Nagy László at Feb 18, 2003 at 9:31 am

    I'm currently trying to receive data from a socket with
    data = mysock.recv(LENGTH)
    the fisrt byte of data is the length of data to receive.
    If I print the length I get : print data[0] -> "\x04"
    How can I "cast" it in order to handle it as an int whose value is 4 ?

    thanks
    You can use the 'ord' and 'chr' functions to convert between a character
    and its code.
    They are using 8bit integers in range(256). If you want to send bigger
    numbers you
    can either

    1. Create your own conversion function (e.g. send more digits with
    radix 256)
    2. Use the Pickle or cPickle to temporary convert the object into a
    string

    Laci 1.0





    From j.heylenAToption.com Tue Feb 18 10:38:06 2003
    From: j.heylenAToption.com (Jan Heylen)
    Date: Tue, 18 Feb 2003 10:38:06 +0100
    Subject: CIM/WMI pcmcia power cycle with python
    Message-ID: <[email protected]>

    Hi,

    I was wondering if it is possible to power off/on a pcmcia slot/card
    from python. Most likely with the wmi interface?

    If it is possible, has somebody done it? Can I see some sample code
    somewhere? Some tips?

    thx

    Jan
    Jan Heylen
    Firmware Engineer

    mailto: j.heylenAToption.com
    http://www.option.com
  • Maciej Sobczak at Feb 18, 2003 at 6:15 pm
    Hi,

    "Franck Bui-Huu" <Franck.BUI-HUU at gemplus.com> wrote in message
    news:mailman.1045559935.23137.python-list at python.org...
    Hi,

    I'm currently trying to receive data from a socket
    I do not want to prevent you from learning sockets (you have to
    go through this stuff anyway), but for network communication
    between two Python programs (and not only) you may find that
    specialized network libraries are very useful and more intuitive
    than raw socket operations.

    Just follow my signature. ;)

    --
    Maciej Sobczak
    http://www.maciejsobczak.com/

    Distributed programming lib for C, C++, Python & Tcl:
    http://www.maciejsobczak.com/prog/yami/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 18, '03 at 9:15a
activeFeb 18, '03 at 6:15p
posts5
users5
websitepython.org

People

Translate

site design / logo © 2023 Grokbase