Python中正数与负数转十六进制

    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]

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值