def calc_hex_value(self, lineedit, weight):
"""
:param lineedit:对应的输入框编号
:param weight:对应输入数值所要求的权值
:return:返回长达十位的十六进制数(D1~D5:前低后高)
"""
if lineedit == '':
value = 0
else:
value = int(decimal.Decimal(str(lineedit)) * weight)
if value >= 0:
data_negative_d1 = str('{:010x}'.format(value))[-2:]
data_negative_d2 = str('{:010x}'.format(value))[-4:-2]
data_negative_d3 = str('{:010x}'.format(value))[-6:-4]
data_negative_d4 = str('{:010x}'.format(value))[-8:-6]
data_negative_d5 = str('{:010x}'.format(value))[-10:-8]
else:
value_positive = -value
value_negative = bin(~value_positive + 1 & 0xffffffffff) # 负数取补码问题
data_negative_d1 = str(hex(int(value_negative, 2))).replace("0x", "").zfill(10)[-2:]
data_negative_d2 = str(hex(int(value_negative, 2))).replace("0x", "").zfill(10)[-4:-2]
data_negative_d3 = str(hex(int(value_negative, 2))).replace("0x", "").zfill(10)[-6:-4]
data_negative_d4 = str(hex(int(value_negative, 2))).replace("0x", "").zfill(10)[-8:-6]
data_negative_d5 = str(hex(int(value_negative, 2))).replace("0x", "").zfill(10)[-10:-8]
return data_negative_d1, data_negative_d2, data_negative_d3, data_negative_d4, data_negative_d5
def swept(self):
swept1 = self.calc_hex_value(self.lineEdit.text(), 10)[0]
swept2 = self.calc_hex_value(self.lineEdit.text(), 10)[1]
Python中正数与负数转十六进制
于 2024-04-15 23:12:37 首次发布