服务器之家

服务器之家 > 正文

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

时间:2021-07-11 00:49     来源/作者:dutsoft

python字符串转数字

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)

执行结果

?
1
2
3
4
5
6
7465737431323334353674657374
2360797289681380981751517517542260
1q6asrk64p36d1l6pq6asrk
2360797289681380981751517517542260
0x7465737431323334353674657374l
test123456test

10进制转n进制

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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

进阶版

?
1
2
3
def basen(num, b):
  return ((num == 0) and "0") or \
     (basen(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])

64进制

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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字符串转数字

?
1
2
biginteger integer = new biginteger(hexstring.tostring(), 16);
integer.tostring(32);
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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进制的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/dutsoft/article/details/79076327

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部