469,949 Members | 2,699 Online

# creating a hex value

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
Jul 19 '05 #1
6 5618
David Bear <da********@asu.edu> writes:
I'm not seeing it in my python essential ref. how can I do

delim = 0x15

delim = chr(0x15)
Jul 19 '05 #2

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'

Jul 19 '05 #3
Paul Rubin wrote:
David Bear <da********@asu.edu> 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.
Jul 19 '05 #4
wi******@hotmail.com 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

Jul 19 '05 #5
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

Jul 19 '05 #6
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