Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signed decimal from a signed hex short in python

I was wondering if theres any easy and fast way to obtain the decimal value of a signed hex in python.

What I do to check the value of a hex code is just open up python in terminal and type in the hex, it returns the decimal like this:

>>>0x0024
36
>>>0xcafe
51966

So I was wondering, is there any way that I could do that for signed short hex? for example 0xfffc should return -4

like image 237
LeedMx Avatar asked May 23 '26 10:05

LeedMx


1 Answers

import ctypes

def hex2dec(v):
    return ctypes.c_int16(v).value

print hex2dec(0xfffc)
print hex2dec(0x0024)
print hex2dec(0xcafe)
like image 121
atline Avatar answered May 24 '26 23:05

atline



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!