Velocity Reviews > creating a hex value

# creating a hex value

David Bear
Guest
Posts: n/a

 06-01-2005
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:
if ln[0] == delim:
do something

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

--
David Bear

Paul Rubin
Guest
Posts: n/a

 06-01-2005
David Bear <(E-Mail Removed)> writes:
> I'm not seeing it in my python essential ref. how can I do
>
> delim = 0x15

delim = chr(0x15)

wittempj@hotmail.com
Guest
Posts: n/a

 06-01-2005

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
>>> hex(21)

'0x15'
>>>

John Machin
Guest
Posts: n/a

 06-01-2005
Paul Rubin wrote:
> David Bear <(E-Mail Removed)> 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
Guest
Posts: n/a

 06-01-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> 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
>
>>>>hex(21)

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

4

Peter Otten
Guest
Posts: n/a

 06-02-2005
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

Fredrik Lundh
Guest
Posts: n/a

 06-02-2005
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:
> 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

</F>