【问题标题】:Converting decimal to Hexa - how do I make it shorter?将十进制转换为 Hexa - 如何使其更短?
【发布时间】:2020-01-27 05:15:22
【问题描述】:

我编写了一个将整数转换为十六进制的代码。这是学校的作业。我写了很多,后来我的一个朋友给我看了他的代码,代码要短得多,这就是为什么我想知道我在哪里过度复杂的东西,以及我可以在哪里削减东西。 顺便说一句,我已经学习了 2 个月了,所以请放轻松:)!

我检查了我的代码,但找不到任何可以删除或简化的内容。

       def Exponent2():                    # which exponent I need to 
                                                           devide with
       global Exponent
       Exponent = 0
       while True:
             a = dezimal // 16**Exponent
             Exponent = Exponent + 1
             if a == 0:
             Exponent = Exponent - 1
             break

        Exponent2()
        Ergebnis = ''
        Dezimal2 = dezimal
        while Dezimal2 != 0:
            dezimal = Dezimal2 // 16**Exponent
            Dezimal2 = Dezimal2 % 16**Exponent
            CheckReverse()
            Ergebnis = Ergebnis + str(dezimal)
            Exponent = Exponent - 1

        Länge = len(Ergebnis)
        Ergebnis = Ergebnis[1:Länge]
        print(Ergebnis)

【问题讨论】:

    标签: python int hex


    【解决方案1】:

    您似乎找到了最大指数x,您可以在其中将数字除以16**x。相反,您可以从可以除以的最小指数开始。换句话说,如果你只除以 16,那么你得到一个数字,然后再除以 16 得到下一个十六进制数字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2015-09-13
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2013-10-30
      相关资源
      最近更新 更多