title: How to convert decimal to bytes?
body: <p>I need to get the seventh root of a very large number in python3. I tried many things but I got overflowed so I thought I'd use decimal and it seems fine to me but I just can't convert it to bytes so I can convert it to base64 after.</p>
  
  <p>Here is my code (<code>c</code> is that large number):</p>
  
  <pre><code>from decimal import *
  import base64
  if len(str(c)) &gt; 25: getcontext().prec = len(str(c))
  else:getcontext().prec =25
  x = Decimal(str(c))
  a = x ** Decimal(1) / Decimal(7)
  res = a.quantize(Decimal('1.'), rounding=ROUND_DOWN)
  res = int.to_bytes(res, length=int(res.bit_length()/8+1), byteorder='big', signed=False)
  print(base64.b64encode(res).decode("utf-8"))
  </code></pre>
  
  <p>But I get this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-48-85e144f30dc7&gt; in &lt;module&gt;()
  ----&gt; 1 res = int.to_bytes(res, length=int(res.bit_length()/8+1), byteorder='big', signed=False)
        2 print(base64.b64encode(res).decode("utf-8"))
  
  AttributeError: 'decimal.Decimal' object has no attribute 'bit_length'
  </code></pre>
  
  <p>I know I should get it. but I just copied the way I used to convert int to bytes.</p>