最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python任意字符串轉(zhuǎn)16, 32, 64進(jìn)制的方法

 更新時(shí)間:2019年06月12日 20:22:32   作者:dutsoft  
今天小編就為大家分享一篇Python任意字符串轉(zhuǎn)16, 32, 64進(jìn)制的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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任意字符串轉(zhuǎn)16, 32, 64進(jìn)制的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中Apriori算法實(shí)現(xiàn)講解

    python中Apriori算法實(shí)現(xiàn)講解

    給大家詳細(xì)講解一下Apriori 算法在python中的實(shí)現(xiàn)過(guò)程,有需要的朋友收藏一下本片文章吧。
    2017-12-12
  • python sys.stdin和sys.stdout的用法說(shuō)明

    python sys.stdin和sys.stdout的用法說(shuō)明

    這篇文章主要介紹了python sys.stdin和sys.stdout的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • 解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯(cuò)誤問(wèn)題

    解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯(cuò)誤問(wèn)題

    這篇文章主要介紹了解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • PyQt5每天必學(xué)之彈出消息框

    PyQt5每天必學(xué)之彈出消息框

    這篇文章主要為大家詳細(xì)介紹了PyQt5每天必學(xué)之彈出消息框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python數(shù)據(jù)預(yù)處理時(shí)缺失值的不同處理方式總結(jié)

    Python數(shù)據(jù)預(yù)處理時(shí)缺失值的不同處理方式總結(jié)

    在使用python做數(shù)據(jù)分析的時(shí)候,經(jīng)常需要先對(duì)數(shù)據(jù)做統(tǒng)一化的處理,缺失值的處理是經(jīng)常會(huì)使用到的。今天介紹的是使用差補(bǔ)法/均值/固定值等不同的方式完成數(shù)據(jù)填充從而保證數(shù)據(jù)的完整性,感興趣的可以了解一下
    2022-12-12
  • 解決python3中的requests解析中文頁(yè)面出現(xiàn)亂碼問(wèn)題

    解決python3中的requests解析中文頁(yè)面出現(xiàn)亂碼問(wèn)題

    requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),編寫(xiě)爬蟲(chóng)和測(cè)試服務(wù)器響應(yīng)數(shù)據(jù)時(shí)經(jīng)常會(huì)用到。這篇文章給大家介紹了解決python3中的requests解析中文頁(yè)面出現(xiàn)亂碼問(wèn)題,感興趣的朋友一起看看吧
    2019-04-04
  • Sanic框架Cookies操作示例

    Sanic框架Cookies操作示例

    這篇文章主要介紹了Sanic框架Cookies操作,結(jié)合實(shí)例形式分析了Sanic框架cookie讀取、寫(xiě)入及刪除等簡(jiǎn)單操作技巧,需要的朋友可以參考下
    2018-07-07
  • Python數(shù)據(jù)存儲(chǔ)之 h5py詳解

    Python數(shù)據(jù)存儲(chǔ)之 h5py詳解

    今天小編就為大家分享一篇Python數(shù)據(jù)存儲(chǔ)之 h5py詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python初識(shí)邏輯與if語(yǔ)句及用法大全

    Python初識(shí)邏輯與if語(yǔ)句及用法大全

    這篇文章主要介紹了Python初識(shí)邏輯與if語(yǔ)句,文中給大家提到了if語(yǔ)句功能及用法講解,需要的朋友可以參考下
    2021-08-08
  • Pytorch模型轉(zhuǎn)onnx模型實(shí)例

    Pytorch模型轉(zhuǎn)onnx模型實(shí)例

    今天小編就為大家分享一篇Pytorch模型轉(zhuǎn)onnx模型實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01

最新評(píng)論

林甸县| 辽中县| 东山县| 新乐市| 绿春县| 溧阳市| 喀什市| 满城县| 长沙县| 丰宁| 剑河县| 衡东县| 民勤县| 克什克腾旗| 亚东县| 隆安县| 惠水县| 灵武市| 沽源县| 庄河市| 遵化市| 涿州市| 义乌市| 吉林市| 合江县| 梅河口市| 额尔古纳市| 漳州市| 枣强县| 栖霞市| 南平市| 盈江县| 曲水县| 林西县| 汾阳市| 石门县| 墨玉县| 农安县| 锡林郭勒盟| 瑞金市| 衡阳县|