Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > creating a hex value

Reply
Thread Tools

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:
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 --
 
Reply With Quote
 
 
 
 
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)
 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      06-01-2005
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'
>>>


 
Reply With Quote
 
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.
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      06-01-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
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

 
Reply With Quote
 
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:
> 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>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hex string to hex value tim Python 8 11-23-2005 06:27 PM
[Fwd: Re: hex string to hex value] tim Python 2 11-23-2005 07:18 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments