append 2 hex values in python

26,394

Solution 1

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.

Solution 2

I don't think you want to "append" them. Doing integer arithmetic by using strings is a bad idea. I think you want to bit-shift a into the right place and OR them together:

>>> a = 0x7B
>>> b = 0x80000
>>>
>>> hex( (a<<20) | b )
'0x7b80000'

Perhaps if you were more specific about what these numbers are and what exactly you're trying to accomplish I could provide a more general answer.

Solution 3

It's been 7 years but the accepted answer is wrong and this post still appears in the first place in google searches; so here is a correct answer:

import math

def append_hex(a, b):
 sizeof_b = 0

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

 # every position in hex in represented by 4 bits
 sizeof_b_hex = math.ceil(sizeof_b/4) * 4


 return (a << sizeof_b_hex) | b

The accepted answer doesn't make sense (you can check it with values a=10, b=1a). In this solution, we search for the nearest divider of 4 - since every hex value is represented by 4 bits - and then move the first value this time of bits.

Solution 4

You can use f-string formatting with Python 3:

>>> a = 0x7b
>>> b = 0x80000
>>> f'0x{a:x}{b:x}'
'0x7b80000'
Share:
26,394
Ram
Author by

Ram

Updated on August 01, 2022

Comments

  • Ram
    Ram almost 2 years

    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

  • Serdalis
    Serdalis about 11 years
    Although this method works for the numbers he has provided, try it on a=0x32, b=0x32. Assuming 20 == bit size of b
  • Ram
    Ram about 11 years
    Thanks!!! that worked. So, I have two hex values and I need to create a string with these values at the correct bit location. Sorry for the misleading question. I can give an overview - I have a file in which each line has two hex values and seeing these values the script should create a string by putting these values at the right location. Ex: ` 32 bit string where 0-8 bits will read 0x7b and bits 10- 30 will read 0x80000. `
  • Ram
    Ram about 11 years
    @ Serdalis - I get it so when I do what you say, it appends zeros at the front.
  • Seng Cheong
    Seng Cheong about 11 years
    @Serdalis Right, it only worked for the values given. I can't think of a reason however, where you would need a general solution like this. Combining numbers without knowing their bit positions? Nonsense.
  • Seng Cheong
    Seng Cheong about 11 years
    @Ram your bit numbers don't make any sense. Bit 0 is the least significant (rightmost) bit.
  • Serdalis
    Serdalis about 11 years
    @JonathonReinhart The values given coincidentally aligned to 4 bits which didn't corrupt the hex values of b. Agreed with the general solution, but if you constrict the input to only ints you just bit shift till the number is 0 and you have the last bit position (all you need for append).
  • BrockenDuck
    BrockenDuck over 3 years
    Thanks for being the only providing a correct answer ! However, how could one make it work when appending 0x00 ?
  • Seng Cheong
    Seng Cheong about 3 years
    @BrockenDuck You can't. That's why the entire question of "appending" values of an unknown size doesn't make sense. What would "appending" 0x00 (or 0x0, or 0x0000000000000) mean to you?
  • BrockenDuck
    BrockenDuck about 3 years
    @JonathonReinhart I guess in this case it would mean that : 0x2F || 0x00 = 0x2F00 ? I am trying to get a larger value, kind of like a multiplication
  • Seng Cheong
    Seng Cheong about 3 years
    0x00 is the exact same as 0 or 0x000000000000000. They're integers of an unspecified size. The fact that you show it with two zeros means nothing from the algorithm's standpoint. That's why, without explicitly stating the size of the integers involved, this entire question is arbitrary!