欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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ì)大家有所幫助。一起跟隨小編過來看看吧

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)文章

  • anaconda虛擬環(huán)境默認(rèn)路徑的更改圖文教程

    anaconda虛擬環(huán)境默認(rèn)路徑的更改圖文教程

    在Anaconda中如果沒有指定路徑,虛擬環(huán)境會(huì)默認(rèn)安裝在anaconda所安裝的目錄下,這篇文章主要給大家介紹了關(guān)于anaconda虛擬環(huán)境默認(rèn)路徑更改的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • Python中Jieba進(jìn)行詞頻統(tǒng)計(jì)與關(guān)鍵詞提取

    Python中Jieba進(jìn)行詞頻統(tǒng)計(jì)與關(guān)鍵詞提取

    本文主要介紹了Python中Jieba進(jìn)行詞頻統(tǒng)計(jì)與關(guān)鍵詞提取,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python基于pycrypto實(shí)現(xiàn)的AES加密和解密算法示例

    Python基于pycrypto實(shí)現(xiàn)的AES加密和解密算法示例

    這篇文章主要介紹了Python基于pycrypto實(shí)現(xiàn)的AES加密和解密算法,結(jié)合實(shí)例形式分析了Python使用pycrypto模塊進(jìn)行AES加密與解密操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-04-04
  • Python實(shí)用技巧之利用元組代替字典并為元組元素命名

    Python實(shí)用技巧之利用元組代替字典并為元組元素命名

    這篇文章主要給大家介紹了關(guān)于Python實(shí)用技巧之利用元組代替字典并為元組元素命名的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧
    2018-07-07
  • 讓Python代碼更快運(yùn)行的5種方法

    讓Python代碼更快運(yùn)行的5種方法

    這篇文章主要介紹了讓Python代碼更快運(yùn)行的5種方法,本文分別介紹了PyPy、Pyston、Nuitka、Cython、Numba等開源軟件,可以提升Python的運(yùn)行效率,需要的朋友可以參考下
    2015-06-06
  • 有關(guān)Python的22個(gè)編程技巧

    有關(guān)Python的22個(gè)編程技巧

    本文給大家分享python的22個(gè)編程技巧,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-08-08
  • python如何生成textgrid文件

    python如何生成textgrid文件

    這篇文章主要介紹了python如何生成textgrid文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • Python簡(jiǎn)單檢測(cè)文本類型的2種方法【基于文件頭及cchardet庫】

    Python簡(jiǎn)單檢測(cè)文本類型的2種方法【基于文件頭及cchardet庫】

    這篇文章主要介紹了Python簡(jiǎn)單檢測(cè)文本類型的方法,結(jié)合實(shí)例形式分析了基于基于文件頭及cchardet庫兩種文本類型檢測(cè)的方法,需要的朋友可以參考下
    2016-09-09
  • Python字符串三種格式化輸出

    Python字符串三種格式化輸出

    這篇文章主要介紹了Python字符串三種格式化輸出,需要的朋友可以參考下
    2020-09-09
  • 跟老齊學(xué)Python之重回函數(shù)

    跟老齊學(xué)Python之重回函數(shù)

    在本教程的開始部分,就已經(jīng)引入了函數(shù)的概念:《永遠(yuǎn)強(qiáng)大的函數(shù)》,之所以那時(shí)候就提到函數(shù),是因?yàn)槲矣X得函數(shù)之重要,遠(yuǎn)遠(yuǎn)超過一般。這里,重回函數(shù),一是復(fù)習(xí),二是要在已經(jīng)學(xué)習(xí)的基礎(chǔ)上,對(duì)函數(shù)有更深刻的理解。
    2014-10-10

最新評(píng)論