0

I have a function that I use to compare two strings character by character and do some additional tasks on it. I want to modify it to compare hex numbers instead.

For example: If A = "hello", B = "himan" were to be compared. I used to run a for loop and compare character by character. It worked fine.

for x, y in zip(A, B):
    if x == y:
        do something

How do I modify it to consider hex numbers. For example, if A = "30303867" and B = "3f160303", I want to match 30 with 3f first then so on. Normally, I can only match 3 by 3 and so on. Thanks

davideej
  • 15
  • 6
  • 1
    [Possible Duplicate](https://stackoverflow.com/questions/1888114/python-hexadecimal-comparison) – W Stokvis Aug 01 '18 at 13:43
  • I would rather not convert my inputs to integer. Its not always possible in my case. – davideej Aug 01 '18 at 14:00
  • 1
    If these values are hex strings, `int(hex_string, 16)` will work. Further explanation can be found [here](https://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python) – W Stokvis Aug 01 '18 at 14:19
  • 1
    @davideej May I know why? If it's not possible to convert them to `int` then the question has nothing to do with comparing `hex` values and it's purely about comparing strings. – machnic Aug 01 '18 at 14:24
  • @machnic I want a general solution since inputs can be strings too. – davideej Aug 01 '18 at 14:27

3 Answers3

0

To get the hex digits of an integer in Python as a string, you would use '{:x}'.format(number). The :x in the format specifier means in hexidecimal format (without the 0x).

Then you can use the string code with the hexadecimal representation.

(Side note: This could also be done with the printf-style format %x)

Edward Minnix
  • 2,889
  • 1
  • 13
  • 26
0

Since you don't want to convert to integers, the easiest thing you can do is iterate through each string two at a time.

for i in range(0, min(len(A),len(B)), 2):
     if A[i:i+2] == B[i:i+2]:
         # Do Something
W Stokvis
  • 1,409
  • 8
  • 15
  • 1
    You would have to make sure, that `A` is the smallest of the two strings. better use `min(len(A), len(B))` as the stopping value. – Tobias Brösamle Aug 01 '18 at 14:14
  • For sanity check, it would be ideal to throw error if the strings are not equal saying `"unequal strings, can't be compared completely"` – Sheldore Aug 01 '18 at 14:20
0

If I understand you correctly, you have two strings A and B, but you want to interpret those strings not as characters but as pairs of hexadecimal digits. But without considering the integer values of those digits. This amounts to comparing pairs of characters, and because they are hexadecimal digits you will need to do a case-insensitive compare because when considered as hexadecimal values, 1F and 1f are equal.

This will split your strings into pairs of characters:

def digit_pairs(s):
    return [s[i:i+2].lower() for i in range(0,len(s),2)]

and then you can do

for a, b in zip(digit_pairs(A),digit_pairs(B)):
    if a == b:
        do something

I don't understand your objection to converting to integer, though. Are you aware that integers in Python can be arbitrarily large?

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Thanks. This is very close to what I was after. Regarding integers, I prefer not to convert to have a more general solution for situations where inputs can't be converted to integer. – davideej Aug 01 '18 at 14:28