kutsurak / python-bitstring

Automatically exported from code.google.com/p/python-bitstring
0 stars 0 forks source link

CreationError: Invalid symbol in hex initialiser. #107

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

I try to search for the bit string '0xc81cc4f8L' integer=3357328632 in a jpg 
file but i get this:

>>> from bitstring import ConstBitStream
>>> s = ConstBitStream(filename='car-8.jpg')
>>> s.find(hex(3357328632))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/bitstring/constbitstream.py", line 123, in find
    t = ConstBitArray.find(self, bs, start, end, bytealigned)
  File "/usr/local/lib/python2.6/dist-packages/bitstring/constbitarray.py", line 1945, in find
    bs = self._converttobitstring(bs)
  File "/usr/local/lib/python2.6/dist-packages/bitstring/constbitarray.py", line 1571, in _converttobitstring
    b._append(ConstBitArray._init_with_token(*tokens[0]))
  File "/usr/local/lib/python2.6/dist-packages/bitstring/constbitarray.py", line 798, in _init_with_token
    b = cls(hex=value)
  File "/usr/local/lib/python2.6/dist-packages/bitstring/constbitarray.py", line 365, in __init__
    self._initialise(auto, length, offset, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/bitstring/constbitarray.py", line 385, in _initialise
    init_without_length_or_offset[k](self, v)
  File "/usr/local/lib/python2.6/dist-packages/bitstring/constbitarray.py", line 1513, in _sethex
    raise CreationError("Invalid symbol in hex initialiser.")
bitstring.errors.CreationError: Invalid symbol in hex initialiser.

Is this a problem with my python version? I am using python 2.6 which seems to 
have a unicode issue. Please advise. Thanks!

Original issue reported on code.google.com by wang...@un0wn.org on 14 Mar 2011 at 10:47

GoogleCodeExporter commented 9 years ago
Hi, the problem is the 'L' character at the end, which isn't a valid hex 
character.

This is used in Python 2.x to indicate a 'long' integer (so the problem will go 
away in Python 3.x).

You can either strip the final 'L' yourself (a bit messy and not recommended) 
or search for the hex or integer directly:

>>> s.find('0xc81cc4f8')
or
>>> s.find('uint:32=3357328632')

If you want to only find them on byte boundaries (and I'm guessing as this is a 
jpeg that you do) then you also need to say 'bytealigned=True' as a parameter 
of find().

Cheers,

Scott

Original comment by dr.scott...@gmail.com on 14 Mar 2011 at 11:03