creating a hex value

Discussion in 'Python' started by David Bear, Jun 1, 2005.

  1. David Bear

    David Bear Guest

    I have a file that I need to parse. Items in it are delimited by a hex 15
    (0x015). I know it must be trivial to assign a hex value to a variable but
    I'm not seeing it in my python essential ref. how can I do

    delim = 0x15
    while:
    ln = file.read()
    if ln[0] == delim:
    do something

    I've looked at the hex function but it doesn't sound like what I want.

    --
    David Bear
    -- let me buy your intellectual property, I want to own your thoughts --
     
    David Bear, Jun 1, 2005
    #1
    1. Advertising

  2. David Bear

    Paul Rubin Guest

    David Bear <> writes:
    > I'm not seeing it in my python essential ref. how can I do
    >
    > delim = 0x15


    delim = chr(0x15)
     
    Paul Rubin, Jun 1, 2005
    #2
    1. Advertising

  3. David Bear

    Guest

    What about

    martin@lijnbaansgracht:~$ python
    Python 2.3.5 (#2, May 4 2005, 08:51:39)
    [GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> hex(21)

    '0x15'
    >>>
     
    , Jun 1, 2005
    #3
  4. David Bear

    John Machin Guest

    Paul Rubin wrote:
    > David Bear <> writes:
    >
    >>I'm not seeing it in my python essential ref. how can I do
    >>
    >>delim = 0x15

    >
    >
    > delim = chr(0x15)


    Ooooh -- a function with a constant arg; I wonder what that evaluates to?

    >>> chr(0x15)

    '\x15'

    Sheeeesh.
     
    John Machin, Jun 1, 2005
    #4
  5. David Bear

    John Machin Guest

    wrote:
    > What about
    >
    > martin@lijnbaansgracht:~$ python
    > Python 2.3.5 (#2, May 4 2005, 08:51:39)
    > [GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >>>>hex(21)

    >
    > '0x15'
    >
    >
    >>> len('0x15')

    4

    Quadruple sheeeeeeeeeeeeeesh.
     
    John Machin, Jun 1, 2005
    #5
  6. David Bear

    Peter Otten Guest

    John Machin wrote:

    >> delim = chr(0x15)

    >
    > Ooooh -- a function with a constant arg; I wonder what that evaluates to?
    >
    > >>> chr(0x15)

    > '\x15'
    >
    > Sheeeesh.


    OK, let's double-check that:

    >>> chr(0x15)

    'The argument is constant -- but not necessarily the /function/.'
    >>>


    Sheeeesh :)

    Peter
     
    Peter Otten, Jun 2, 2005
    #6
  7. David Bear wrote:

    >I have a file that I need to parse. Items in it are delimited by a hex 15
    > (0x015). I know it must be trivial to assign a hex value to a variable but
    > I'm not seeing it in my python essential ref. how can I do
    >
    > delim = 0x15
    > while:
    > ln = file.read()
    > if ln[0] == delim:
    > do something
    >
    > I've looked at the hex function but it doesn't sound like what I want.


    you can use use

    ord(ln[0]) == delim

    or

    ln[0] == '\x15'

    or

    ln[0] == chr(delim)

    or

    ln.startswith("\x015")

    or some other variation.

    fwiw, I'm pretty sure file.read() doesn't do what you want either (unless
    you're 100% sure that the file only contains a single item).

    if the file isn't larger than a few megs, consider using

    items = file.read().split("\x15")

    </F>
     
    Fredrik Lundh, Jun 2, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,357
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    506
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    605
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    19,060
  5. tim
    Replies:
    2
    Views:
    1,589
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page