Python转C#参考样例
最近在做一部分Pyhton代码转c#代码的工作,以下案例亲自都测试过,现整理出来希望对有帮助的同学提供参考:
Python | C#
Python | C# |
---|---|
datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S') | DateTime.Now.ToString("yyyyMMddHHmmss") |
random.choice('123456789') | random.Next(1, 9).ToString() |
struct.pack('>I', int(time.time())) | TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); byte[] timeSpanBytes = BitConverter.GetBytes(Convert.ToUInt32(ts)); if (BitConverter.IsLittleEndian) { Array.Reverse(timeSpanBytes); } |
binascii.hexlify(ab) | BitConverter.ToString(timeSpanBytes) |
random.randint(0, 100000000)) | Random random = new Random(DateTime.Now.Millisecond); random.Next(0, 100000000) |
myhmac = hmac.new("d6fc3a4a06adbde89223bvefedc24fecde188aaa9161",digestmod=hashlib.sha1) myhmac.update(binascii.unhexlify('57b47f0a1b8a35a00300fbe94bcf')) encode=base64.b64encode(myhmac.digest()) |
string hexData = "57b47f0a1b8a35a00300fbe94bcf"; if(hexvalue.Length % 2 != 0) { hexvalue = "0" + hexvalue; } int len = hexvalue.Length / 2; byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { string byteString = hexvalue.Substring(2 * i, 2); bytes[i] = Convert.ToByte(byteString, 16); } string str = "d6fc3a4a06adbde89223bvefedc24fecde188aaa9161"; ASCIIEncoding encoder = new ASCIIEncoding(); Byte[] code = encoder.GetBytes(str); HMACSHA1 hmSha1 = new HMACSHA1(code); Byte[] hmBytes = hmSha1.ComputeHash(bytes); string encode = Convert.ToBase64String(hmBytes); |
bytes=binascii.unhexlify(hexvalue) | if (hexvalue.Length % 2 != 0) { hexvalue = "0" + hexvalue; } int len = hexvalue.Length / 2; byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { string byteString = hexvalue.Substring(2 * i, 2); bytes[i] = Convert.ToByte(byteString, 16); } return bytes; |
var hmac=hashlib.md5('F%s%s' % (time_str, device_no)).hexdigest() | var md5 = new MD5CryptoServiceProvider(); byte[] m =md5.ComputeHash(Encoding.UTF8.GetBytes($"F{timeSpan}{deviceNO}")); var hmac = BitConverter.ToString(m).Replace("-", "").ToLower(); |
buf_size = 0x1000 raw_memory = bytearray(buf_size) ctypes_raw_type = (ctypes.c_char * buf_size) ctypes_raw_memory=ctypes_raw_type.from_buffer(raw_memory) encLen = Objdll.encode(byref(ctypes_raw_memory),buf_size,inputCode,len(inputCode))#Objdll.encode为c++调用# return raw_memory[:encLen] |
IntPtr data = Marshal.StringToHGlobalAnsi(inputCode); byte[] aaab = new byte[4096]; int aa = encode(aaab, 4096, data, inputCode.Length);byte[] byteNew = new byte[aa]; for (int i = 0; i < aa; i++) { byteNew[i] = aaab[i]; } return byteNew; |
szPara = create_string_buffer('/0'*buf_size) decLen = Objdll.decode(byref(szPara), buf_size,decodeInput,len(decodeInput)) #Objdll.encode为c++调用# return szPara.value[:decLen] |
byte[] outsting = new byte[0x1000]; int encLen = decode(outsting, outsting.Length, inputCode, inputCode.Length); String ret = Encoding.UTF8.GetString(outsting, 0, encLen); return ret; |
json.loads(test) | JsonConvert.DeserializeObject(test) |
作者:tuohaibei
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利.
如果您觉得文章对您有帮助,可以点击文章右下角"推荐".您的鼓励是作者坚持原创和持续写作的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?