append 2 hex values in python

hex, python, string

Solution

This is a more generic way to append `hex` / `int` / `bin` values. Only works for positive values of `b`.

a = 0x7b
b = 0x80000

def append_hex(a, b):
    sizeof_b = 0

    # get size of b in bits
    while((b >> sizeof_b) > 0):
        sizeof_b += 1

    # align answer to nearest 4 bits (hex digit)
    sizeof_b += sizeof_b % 4

    return (a << sizeof_b) | b

print(hex(append_hex(a, b)))

Basically you have to find the highest set bit that `b` has. Align that number to the highest multiple of `4` since that's what `hex` chars are. Append the `a` to the front of the highest multiple of 4 that was found before.

Problem

I am trying to append some hex values in python and I always seem to get 0x between the number. From what I searched, either this is not possible without converting it into a lit of values ?? I am not sure. ``` a = 0x7b b = 0x80000 hex(a) + hex(b) = 0x7b0x80000 ``` I dont want the 0x in the middle - I need, `0x7b80000`. is there any other way to do this? If I convert to integer I get the sum of the two and converting it to hex is a different value than `0x7b80000`

Original source