Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append 2 hex values in python

Tags:

python

string

hex

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

like image 949
Ram Avatar asked Mar 26 '13 04:03

Ram


People also ask

How do you add two hex values in Python?

In python, there are inbuilt functions like hex() to convert binary numbers to hexadecimal numbers. To add two hexadecimal values in python we will first convert them into decimal values then add them and then finally again convert them to a hexadecimal value. To convert the numbers make use of the hex() function.

How do I print a hex value without 0x in Python?

To print a positive or negative hexadecimal without the '0x' or '-0x' prefixes, you can simply use the string. replace('x', '0') method and replace each occurrence of 'x' with '0' .


3 Answers

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.

like image 86
Jonathon Reinhart Avatar answered Sep 19 '22 08:09

Jonathon Reinhart


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.

like image 20
Serdalis Avatar answered Sep 20 '22 08:09

Serdalis


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.

like image 35
Algafix Avatar answered Sep 19 '22 08:09

Algafix