On Sun, 31 May 2015 11:36:35 -0700, fl wrote: > I am new to Python. I would manipulate a string of hex numbers. If the > first digit is bigger than 7, the first two digits are required to add > 4.
What happens if the first two digits are ff, does it become 103 or 03. If you have ffff_ffff_ffff Do you want to create 103ff_103ff_103ff or 03ff_03ff_03ff or 0400_0400_03ff or 10400_0400_03ff ...... > For example, '8022_3345' will be changed to '8422_3345'. The underscore > between two 4-digit's was generated previously (i.e. > it is already in the .txt file). > > I have not tried to read the .txt file to a list yet. I just try the > following: > > tmp ['12345678', '23456789', '3456789a', '456789ab'] > > Each 8-digit hex number is assigned to a variable, such as: > > digit8=tmp[0] > > I can compare digit8[0] with 7, and so on... > > The underscore, I think, can be removed by first a string replace. > > My question here is: > > Do you have better ways than my tmp, digit8 operation? Yes, if these are numbers, manipulate them as numbers, not strings. def bing(n): n = int(n.replace("_", ""), base=16) # convert to numbers if n > 0x7fffffff: # if 0x80000000 or more n = n + 0x04000000 # add 0x04000000 return n # and return result newnums = [ bing(x) for x in oldnums ] It could probably be done as a single list comprehension, but it might get a bit messy. -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list