小編給大家分享一下Python如何實現(xiàn)任意字符串轉(zhuǎn)16, 32, 64進(jìn)制,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)公司于2013年成立,先為嵊州等服務(wù)建站,嵊州等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為嵊州企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。Python字符串轉(zhuǎn)數(shù)字
import binascii s = 'test123456test' str_16 = binascii.b2a_hex(s.encode('utf-8')) # 字符串轉(zhuǎn)16進(jìn)制 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進(jìn)制轉(zhuǎn)10進(jìn)制 print(num_10) str_32 = baseN(num_10, 32) # 10進(jìn)制轉(zhuǎn)32進(jìn)制 print(str_32) num_10_2 = int(str_32, 32) # 32進(jìn)制轉(zhuǎn)10進(jìn)制 print(num_10_2) num_16 = hex(num_10) # 10進(jìn)制轉(zhuǎn)16進(jìn)制數(shù) print(num_16) ss = str_16.decode('hex') # 16進(jìn)制串轉(zhuǎn)字符串 print(ss)
執(zhí)行結(jié)果
7465737431323334353674657374 2360797289681380981751517517542260 1q6asrk64p36d1l6pq6asrk 2360797289681380981751517517542260 0x7465737431323334353674657374L test123456test
10進(jìn)制轉(zhuǎn)n進(jì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
進(jìn)階版
def baseN(num, b): return ((num == 0) and "0") or \ (baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])
64進(jìn)制
def encode_b64(n): table = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_' result = [] temp = n if 0 == temp: result.append('0') else: while 0 < temp: result.append(table[temp % 64]) temp /= 64 return ''.join([x for x in reversed(result)]) def decode_b64(str): table = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "a": 10, "b": 11, "c": 12, "d": 13, "e": 14, "f": 15, "g": 16, "h": 17, "i": 18, "j": 19, "k": 20, "l": 21, "m": 22, "n": 23, "o": 24, "p": 25, "q": 26, "r": 27, "s": 28, "t": 29, "u": 30, "v": 31, "w": 32, "x": 33, "y": 34, "z": 35, "A": 36, "B": 37, "C": 38, "D": 39, "E": 40, "F": 41, "G": 42, "H": 43, "I": 44, "J": 45, "K": 46, "L": 47, "M": 48, "N": 49, "O": 50, "P": 51, "Q": 52, "R": 53, "S": 54, "T": 55, "U": 56, "V": 57, "W": 58, "X": 59, "Y": 60, "Z": 61, "-": 62, "_": 63} result = 0 for i in xrange(len(str)): result *= 64 result += table[str[i]] return result
Java字符串轉(zhuǎn)數(shù)字
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如何實現(xiàn)任意字符串轉(zhuǎn)16, 32, 64進(jìn)制”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:Python如何實現(xiàn)任意字符串轉(zhuǎn)16,32,64進(jìn)制-創(chuàng)新互聯(lián)
文章地址:http://jinyejixie.com/article6/hgeig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、商城網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站、品牌網(wǎng)站制作、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容