# creating a hex value

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

1. ### David BearGuest

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

David Bear, Jun 1, 2005

2. ### Paul RubinGuest

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

3. ### Guest

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'
>>>

, Jun 1, 2005
4. ### John MachinGuest

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
5. ### John MachinGuest

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

John Machin, Jun 1, 2005
6. ### Peter OttenGuest

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
7. ### Fredrik LundhGuest

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