一个C# 代码求大神转Python

JeruSalem 2019-05-25 06:11:40
       
public static string HexToBase64(string hexStr)
{
MatchCollection mc = Regex.Matches(hexStr.ToString(), "[A-F0-9]{2}");
byte[] bytes = new byte[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
bytes[i] = byte.Parse(mc[i].Value, System.Globalization.NumberStyles.HexNumber);
}
return Convert.ToBase64String(bytes);
}
...全文
317 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
这段代码的作用就是将16进制化的字符串转成byte[]后再转成base64字符串格式
stherix 2019-05-25
  • 打赏
  • 举报
回复
import re
import base64

def HexToBase64(hexStr):
    reg = re.compile("[A-F0-9]{2}")
    matches = reg.findall(hexStr)
    bytes = bytearray(0)
    for m in matches:
        b = int(m,16)
        bytes.append(b)
    return base64.b64encode(bytes)
橘子皮... 2019-05-25
  • 打赏
  • 举报
回复
sorry,py不太熟,上面的错了,应该是下面的

import re
import base64

s = "%E6%88%91"
pattern1 = re.compile("[A-F0-9]{2}")
a= pattern1.findall(s)

aa = bytearray(len(a))
for L in a:
    n = int(L,16)
    aa.append(n)

s = str(aa,'utf-8');print(s)
print(base64.b64encode(aa))
橘子皮... 2019-05-25
  • 打赏
  • 举报
回复

import re
import base64

s = "F0&0F"
pattern1 = re.compile("[A-F0-9]{2}")
a= pattern1.findall(s)

aa = []
for L in a:
    n = int(L,16)
    aa.append(n)

s = str(aa,'utf-8')
print(base64.b64encode(s))

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧