Is there a way to do math in hex without converting it to and from int?
You have to explain what you mean by "in hex". int is a number. hexadecimal, octal, decimal, and binary are just ways we can look at that number.
More on reddit.comIs there a way to do math with hexadecimals in Python 3? - Stack Overflow
python - how to do a hexadecimal increment? - Stack Overflow
python - Adding/Subtracting Hexadecimals - Stack Overflow
>>> a = 0xA
>>> b = 0xa
>>> a + b
20
>>> hex(a + b)
'0x14'
Hexadecimal and decimal, or any other type of encoding, really, is fundamentally just 1's and 0's. 0xa0 is identical to 160 which is identical to 0b10100000 and so on. When you do "math with hexadecimal," the "with hexadecimal" is entirely peripheral. If you have a hexidecimal string such as "0xa0" and want to convert that into a number that you can use, then just do something like:
def from_hex(hexdigits):
return int(hexdigits, 16)
x = "0xa0"
y = "0x10"
z = hex(from_hex(x) + from_hex(y))
print(z) # => 0xb0
There's no real way around it unless you want to create your own custom subclass from int which just isn't worth it.
» pip install xalc
The int builtin has an optional base parameter, which you can use to read hex values.
with open(r'\\network\loc\build_ver.txt','r+') as f:
value = int(f.read(), 16)
f.seek(0)
f.write(hex(value + 1))
You can use hex for base 16 output or str for base 10 output.
>>> val = int("9a", base=16)
>>> val
154
>>> hex(val + 1)
'0x9b'
>>> str(val + 1)
'155'
It's also worth noting that you should either validate the input or have a try block somewhere:
>>> int("g", 16)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 16: 'g'
Hex is in Python is stored in the form of strings. Incrementing is done on integers. So you just need to convert to an integer and back:
>>> h = '0x3f8' # hex string
>>> i = int(h, 16) # convert to an integer
>>> i += 1 # increment
>>> hex(i) # convert back to hex string
'0x3f9'
Hope this solves your problem neatly :-)
Thanks to @Peri461
basehex = input()
sechex = input()
basehexin = int(basehex, 16)
sechexin = int(sechex, 16)
sum = basehexin - sechexin
print(hex(sum))
This code will do it, by converting the hexadecimals to decimals, subtracting them, then converting(representing) them to hexadecimals again.
Zombie! but this was the first google hit so I am adding on here.
If you can convince the user to enter in hex format (0x100) you can just do normal math.
basenum = 0x100
addnum =0x10
total = basenum + addnum
To represent as hexadecimal, use binascii.hexlify:
>>> import binascii
>>> binascii.hexlify('\x03\x2F\x00\x00\x02\x12\x01\x47')
'032f000002120147'
>>> ' '.join(binascii.hexlify(ch) for ch in '\x03\x2F\x00\x00\x02\x12\x01\x47')
'03 2f 00 00 02 12 01 47'
To calculate checksum, iterate each bytes:
data = '\x03\x2F\x00\x00\x02\x12\x01\x47'
checksum = 0
for ch in data:
checksum += ord(ch) # ord('\x03`) -> 3
or using sum and map:
checksum = sum(map(ord, data))
Do you expect to see this:?
input ='\x03\x2F\x00\x00\x02\x12\x01\x47'
output = ' '.join([('{:02X}'.format(ord(c))) for c in input])
print(output)
prints:
03 2F 00 00 02 12 01 47
If not - clarify your question