• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python任意字符串转16, 32, 64进制的方法

python 搞代码 4年前 (2022-01-07) 47次浏览 已收录 0个评论

今天小编就为大家分享来源gao($daima.com搞@代@#码网一篇Python任意字符串转16, 32, 64进制的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

Python字符串转数字

 import binascii s = 'test123456test' str_16 = binascii.b2a_hex(s.encode('utf-8')) # 字符串转16进制 print(str_16) def baseN(num, b): return ((num == 0) and "0") or \ (baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b]) num_10 = int(str_16, 16) # 16进制转10进制 print(num_10) str_32 = baseN(num_10, 32) # 10进制转32进制 print(str_32) num_10_2 = int(str_32, 32) # 32进制转10进制 print(num_10_2) num_16 = hex(num_10) # 10进制转16进制数 print(num_16) ss = str_16.decode('hex') # 16进制串转字符串 print(ss)

执行结果

 7465737431323334353674657374 2360797289681380981751517517542260 1q6asrk64p36d1l6pq6asrk 2360797289681380981751517517542260 0x7465737431323334353674657374L test123456test

10进制转n进制

 def base10toN(num,n): """Change a to a base-n number. Up to base-36 is supported without special notation.""" num_rep={10:'a', 11:'b', 12:'c', 13:'d', 14:'e', 15:'f', 16:'g', 17:'h', 18:'i', 19:'j', 20:'k', 21:'l', 22:'m', 23:'n', 24:'o', 25:'p', 26:'q', 27:'r', 28:'s', 29:'t', 30:'u', 31:'v', 32:'w', 33:'x', 34:'y', 35:'z'} new_num_string='' current=num while current!=0: remainder=current%n if 36>remainder>9: remainder_string=num_rep[remainder] elif remainder>=36: remainder_string='('+str(remainder)+')' else: remainder_string=str(remainder) new_num_string=remainder_string+new_num_string current=current/n return new_num_string

进阶版

 def baseN(num, b): return ((num == 0) and "0") or \ (baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])

64进制

 def encode_b64(n): table = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_' result = [] temp = n if 0 == temp: result.append('0') else: while 0 </div><p><strong>Java字符串转数字</strong></p><div class="gaodaimacode"><pre class="prettyprint linenums"> BigInteger integer = new BigInteger(hexString.toString(), 16); integer.toString(32);
 import java.math.BigInteger; public class Main { public static void main(String[] argv) throws Exception { BigInteger bi = new BigInteger("1023"); bi = new BigInteger("1111111111", 2); String s = bi.toString(2); System.out.println(s); bi = new BigInteger("1000", 8); System.out.println(s = bi.toString(8)); bi = new BigInteger("1023"); s = bi.toString(); System.out.println(s); bi = new BigInteger("3ff", 16); s = bi.toString(16); System.out.println(s); } }

以上这篇Python任意字符串转16, 32, 64进制的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是Python任意字符串转16, 32, 64进制的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python任意字符串转16, 32, 64进制的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址