469,949 Members | 2,699 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,949 developers. It's quick & easy.

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


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:
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.

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

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tarscher | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.