简体   繁体   中英

How do you convert hexadecimal to ternary in Python?

I'm trying to convert any length hexadecimal to ternary (two way), specifically in Python. I've tried using int , and that didn't work. I also tried this library called baseconvert , but that just spits an error, NameError: name 'base' is not defined , when I try to use the base command they give.

Then, I tried this script:

#!/usb/bin/python

def toStr(n,base):
   convertString = "0123456789ABCDEF"
   if n < base:
      return convertString[n]
   else:
      return toStr(n//base,base) + convertString[n%base]

#####################

hex = "6a48f82d8e828ce82b82abc12397812389721389712387912321387913248723081270243872308472384723472304723089547230485728347283470928347087230457340587304857034570345708345034509348509834509834598230948230948203948092348092348092348092385409283092340923409823490823409820394820934809234809abababababab2345098234092349084238903244444444444444444444444444444442430898888888888888888888999999999999999999999997"
# hex = "6A48F82D8E828CE82B82"
# hex = "e30ac3baf3ab3ffedb8a02dfcc962e2c455650c1614d63ba9a058685da6f04f1c282a5214e65a47506d65a7b9a80d85fc7365aabce539a0696ff2157485d720a7368613531323a20646333656537636164333839396538333839316236376137376136356531356362346665383262356465646363623631373237383833633761306232663064366630396461643264316461393937336432663134333338393161653264373534623864316363653835656433656635353865363634626665656135373531363820696e6465782e68746d6c"

print("hex  =", hex)

# dec = int(hex, 16)
# print("dec  =", dec)

tern = (toStr(int(hex, 16),3))
print("tern =", tern)

rehex = (toStr(int(tern, 3),16))
print("rehex  =", rehex)

#####################


# unhex: Convert Hexadecits to Trits
# tern = ""
# for i in hex:
#     digit = (toStr(int(i, 16),3))
#     tern += digit
#     # print("tern =", tern)

#     # print(i)
# print("tern =", tern)

Which does it two-way, but shows this error: RecursionError: maximum recursion depth exceeded in comparison . I want to have any length, so I can't just increase the maximum recursion depth, because that still won't work with every length.

Any help is appreciated!

Your code works as expected, and does what you want it to do. The problem is that Python by default has a guard for maximum recursion depth (maximum 1000 layers in stack). You can increase it, but this would not be very scalable. What you really want to do is rewrite your code in an iterative fashion.

Your code could look something like that:

def toStr(n, base):
    convertString = "0123456789ABCDEF"
    result = ''
    while n > base:
        result = result + convertString[n%base]
        n = n//base
    if n > 0:
        result = result + convertString[n]
    return result

More info: SO description on max recursion depth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM