9

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

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Although this method works for the numbers he has provided, try it on a=0x32, b=0x32. Assuming 20 == bit size of b
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. `
@ Serdalis - I get it so when I do what you say, it appends zeros at the front.
@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.
@Ram your bit numbers don't make any sense. Bit 0 is the least significant (rightmost) bit.
|
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.

4 Comments

Thanks for being the only providing a correct answer ! However, how could one make it work when appending 0x00 ?
@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?
@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
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!
3

You can use f-string formatting with Python 3:

>>> a = 0x7b
>>> b = 0x80000
>>> f'0x{a:x}{b:x}'
'0x7b80000'

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.